MySQL Forums :: PHP :: Subquery and JOIN's


Advanced Search

Subquery and JOIN's
Posted by: Jason Heddings ()
Date: July 17, 2009 04:57PM

I have the following layout in 2 tables:

data => id, owner, time, value
owners => id, name

I'm trying to create a query to get the most recent update in the `data` table and return the name in the `owners` table with the value. To get the most recent update for each owner, I have the following query (which seems to be working well):

SELECT * FROM (SELECT * FROM `data` ORDER BY `time` DESC) d1 GROUP BY `owner`;

Now I'm having trouble creating the statement to add the owner name to the result. I attempted the following statement, but it does not seem to work:

SELECT * FROM (SELECT * FROM (SELECT * FROM `data` ORDER BY `time` DESC) d1 GROUP BY `owner`) d2, `owners` WHERE (d2.owner = owners.id);

This look terribly inefficient to me, but I can't think of another way to accomplish the same goal. I'm a bit new to SQL programming, so there may be a more sophisticated way to accomplish this. Thanks for any help!

Options: ReplyQuote


Subject Written By Posted
Subquery and JOIN's Jason Heddings 07/17/2009 04:57PM
Re: Subquery and JOIN's Rick James 07/18/2009 06:12PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.