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


Quote Reply
Re: Select 2 fields from 2 db's, merge and sort In reply to
Are you using LINKS SQL v.1.3 or 2.X? If so, then you only needed to post this Thread in this forum and not in the Links SQL v.1.3 forum.

Regards,

Eliot Lee
Quote Reply
Re: Select 2 fields from 2 db's, merge and sort In reply to
I'm using version Links Sql 1.13, I thought I'd post it in both forums because the concept of what I'm doing seems pretty much the same as this isn't so much an SQL version specific function, but rather a perl sort function.

My apologies for being redundant.

Peace.

Kyle