Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Select 2 fields from 2 db's, merge and sort

Quote Reply
Select 2 fields from 2 db's, merge and sort
Hi There,
This is really a links sql1.x question, but it applies to both versions and since I'm the first post in that forum since April 3 I'm posting it in both areas.

Here goes:

For the last two days I have been trying to do two distinct selects from 2 distinct tables on fields that are named the same, then combine them into 1 array and sort it.

These are my queries
$sth = $LINKDB->prepare ("SELECT Series,ID FROM Links where CategoryID = '18' ORDER BY Series");
$adt = $LINKDB->prepare ("SELECT a.Series,c.ID FROM Links as c, Links_Additional_Dates as a where (c.ID=a.ID) and c.Series > '2001-04-31' and c.Add_Date > '2001-04-01' ORDER BY a.Series");

Links contains 31 fields or so.
Links_Additional_Dates contains ID and Series - ID is the same ID as found in Links


Then I read them into an array like this

while ($event = $sth->fetchrow_hashref) {
push @$events, $event;
}
while ($event = $adt->fetchrow_hashref) {
push @$events, $event;
}

Now I want to resort the array according to the dates as found in the Series column but I can't figure out how to sort it with something like:
foreach my $tmp(sort @$events) {.....

I've tried a hundred variations of sort(I'm not kidding) but can't seem to get it to sort by anything other than the address which looks like this HASH(0x827c974)

Once I step through the array I can access both tmp->{Series} and tmp->{ID}

I'm either reading @$events wrong, or I am doing something wrong in the sort. I've tried this:

foreach my $tmp (sort { ($events->{$a}->{Series} <=> $events->{$b}->{Series}) } @events) {...
and that just tells me NOT a HASHREF.

Does anyone have an idea how to sort that array on something other than the address or have a suggestion on
how to read into those two fields into a hash properly so that I can sort by the Series which are actually dates?

Thanks - you are a hero.

Kyle


Subject Author Views Date
Thread Select 2 fields from 2 db's, merge and sort klangan 1433 May 2, 2001, 7:56 AM
Thread Re: Select 2 fields from 2 db's, merge and sort
Stealth 1384 May 2, 2001, 9:07 AM
Post Re: Select 2 fields from 2 db's, merge and sort
klangan 1386 May 2, 2001, 9:53 AM