MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 6th, 2000, 06:14 AM
leaed leaed is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2000
Posts: 12 leaed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I've got a table which contains comments to images. The table has an id field (auto number), a field referencing the image the comment is about, the comment its self and the date of the comment. The table is called comments. I want to select the most recent comment for each image_id. But I can't, I've tried all sorts of GROUP BYs on image_id and used MAX(postdate) or MAX(id) but I can't get the right comment out. I've tried selecting MAX(postdate) AS something and then using HAVING something, but the all have this of course, so I don't know. It doesn't help that MySQL doesn't seem to be able to ORDER BY and then GROUP BY a select.

Can anyone help? There has to be an easy solution to stop me losing sleep...

Reply With Quote
  #2  
Old September 6th, 2000, 09:27 AM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
Try this.

select comment,max(postdate) from comments group by image_id;

Reply With Quote
  #3  
Old September 6th, 2000, 10:22 AM
leaed leaed is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2000
Posts: 12 leaed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
That seems to give the latest "postdate", but the first comment. I want to get the comment which corresponds to MAX(postdate), what you suggested was what I tried first - it doesn't seem to be working.

Reply With Quote
  #4  
Old September 6th, 2000, 01:02 PM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
Hmm, odd.

Try this then:

select comment from comments where postdate=max(postdate) group by image_id;

Reply With Quote
  #5  
Old September 6th, 2000, 02:26 PM
leaed leaed is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2000
Posts: 12 leaed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
that's another thing i've tried, it produces a really crappy mySQL error which doesn't help. What I really need is a sub-select, its the only way I can join against MAX(postdate), as far as I can see. I was playing at work today, trying to do it in Access (out of desperation) and that has more functions which can be used with GROUP BY, including LAST(), which does what I need it do, i.e. if I do LAST(comment) it gives it to me. mySQL doesn't seem to have anything like this... I know Access can never out-do mySQL - there must be solution, somewhere.

Reply With Quote
  #6  
Old September 6th, 2000, 02:33 PM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
Perhaps if you posted the "crappy mysql error"....

details, details, details if you want help

Reply With Quote
  #7  
Old September 6th, 2000, 03:19 PM
leaed leaed is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2000
Posts: 12 leaed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
the message is

MySQL said: Invalid use of group function

not very useful, is it?

Reply With Quote
  #8  
Old September 6th, 2000, 05:43 PM
rod k rod k is offline
Apprentice Deity
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jul 1999
Location: Niagara Falls (On the wrong side of the gorge)
Posts: 3,237 rod k User rank is Private First Class (20 - 50 Reputation Level)rod k User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 m 8 sec
Reputation Power: 13
Send a message via AIM to rod k
Yeah, I had my doubts about whether that would work or not.

That brings us back to the first query. That SHOULD work. I tried it myself and it worked fine... of course, I had to guess at your data structure. If you'd like, email me a schema/data dump of the table and I'll take a look.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > GROUP BY : getting what I need


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway