Page is a not externally linkable
- Code, Content, and Presentation
-- Databases
---- MySQL - Group By Problem


melar09 - 2:29 pm on Sep 13, 2010 (gmt 0)


Okay I have two database tables, one called pictures and another called picture cats. What I want to do is fetch the most recent picture for each cat along with the cat information so it would look like this...

Picture Gallery
- Some Cat
[Newest Pic for some cat]
- Another Cat
[Newest Pic for this cat]
and so on...


Here is what I got so far...
SELECT cats.*, pics.* FROM cats JOIN pics ON (pics.cat_id=cats.id) GROUP BY cats.id ORDER BY cats.timestamp, pics.timestamp DESC

But it returns the list of cats with the first added picture for each cat. I have searched for a bit and have tried a few different things I have a feeling I have to do a sub query but I am not very good with them yet. That is also not my exact query as far as table names etc but its setup like that.


Thread source:: http://www.webmasterworld.com/databases_sql_mysql/4201161.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com