...

Join problem on MySQL

Wonderm00n
09-10-2002, 12:24 PM
MYSQL: 3.23.36 running on LINUX

I've got two tables:

Table: Titles
Fields: ID, title, index

Table: Values
Fields: ID_title, value, hour


The first table has the unique list of Titles, the second table has
the values each title has on a specific hour. There are several
records on the second table matching the first table. How can I select
the LAST value for each title on the first table, that has
index="abc"? (LAST means with the bigger hour)

I've tried:
-> SELECT Titles.ID, Titles.title, Values.value, Values.hour from
Titles, Values where Values.ID_title=Titles.ID and Titles.index='abc'
group by Titles.ID
It returns the firsts values for each title found on the second table.

-> SELECT Titles.ID, Titles.title, Values.value, Values.hour from
Titles, Values where Values.ID_title=Titles.ID and Titles.index='abc'
group by Titles.ID order by Values.hour
It returns the same values that the one above but ordered by hour
DESC.

-> SELECT Titles.ID, Titles.title, Values.value, max(Values.hour) from
Titles, Values where Values.ID_title=Titles.ID and Titles.index='abc'
group by Titles.ID order by Values.hour
It returns the hour's that I want, but the values are still the first
ones and not the ones related to the hour returned...

-> SELECT Titles.ID, Titles.title, Values.value, Values.hour from
Titles, Values where Values.ID_title=Titles.ID and Titles.index='abc'
It returns all the values with repective hour for all the titles. (If
I could just filter theese results to get just the last value for each
Title)

Any ideas on how to do this??
I'm starting to crash my head on the wall on this...

PS: Sorry about the bad english

Spookster
09-10-2002, 03:18 PM
What kind of datatype is the field for the hour?

Wonderm00n
09-10-2002, 03:22 PM
The data type for "hour" is time

Dylan Leblanc
09-17-2002, 11:37 PM
you could try

SELECT Titles.ID
FROM Titles LEFT JOIN Values ON Titles.ID = Values.ID_title
WHERE Titles.value = 'abc'
ORDER BY hour DESC LIMIT 1

which will order the results by the numberical value of hour and do that in descending order, and then limits the returned result to only one row, being the one you want.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum