Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-11-2009, 01:27 AM   PM User | #1
skywaves
New Coder

 
Join Date: Jun 2009
Posts: 28
Thanks: 4
Thanked 0 Times in 0 Posts
skywaves is an unknown quantity at this point
order by and gorup by in single sql statement

Hi i have a table subforum with structure as follows

subforumid - int auto increment
forumid -int
userid -int
projectid -int
subject - longtext
createdate - datetime
deleted - int

now i want to display the number of replies and the last post's time.

Code:
select createdate, forumid, count(subforumid) from subforum  group by forumid order by createdate desc

it runs but does not give expected result. it order the createdate in result created by
Code:
select createdate, forumid, count(subforumid) from subforum  group by forumid
i know i am doing something wrong here but cannot figure out.
do i have to use multiple queris?

thanks
skywaves is offline   Reply With Quote
Old 12-11-2009, 01:58 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ahhh, you are going to kick yourself over this one.
Code:
SELECT forumid, MAX(createdate) AS lastCreatedSubforum, COUNT(*) AS numberOfSubforums
FROM subforum
GROUP BY forumid
ORDER BY lastCreatedSubforum DESC
You were done in by a "feature" of MySQL. Most DBs would have given you an error from that query, because you did GROUP BY *only* of forumid and did not put createdate into either an aggregate function or into the group by. MySQL is lenient, but then doesn't give you the answer you expect.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
skywaves (12-11-2009)
Reply

Bookmarks

Tags
count, group by, order by, sort

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:00 AM.


Advertisement
Log in to turn off these ads.