Jump to content

group by & order by


PRSBOY

Recommended Posts

Hey there,

 

I'm trying to whip up some forums, only basic and secure. Here are my involved tables:

 

forumthread

threadid 	 	 
threadviews	 
groupid	 
forumid 	 
threadtitle	 
threadsticky 	 
userid

 

threadid is the primary key, auto_inc.

 

forumpost

postid 	 
threadid	 	 
forumid 	 
userid 	 	 
groupid	 	 
postbody 	 
postdate

 

postid is the primary key, auto_inc.

threadid is the foreign key linking to a specific thread.

 

What I am trying to do, is print the each thread, listing its number of replies (count(postid)), last poster details (ORDER BY forumpost.postdate DESC)

 

The SQL I has was:

SELECT
p.*
t.*
FROM forumpost p
LEFT JOIN forumthread t ON p.threadid = t.threadid
GROUP BY p.threadid
ORDER BY p.postdate DESC

 

The problem is: the query selects the correct number of rows, however, it does not return the correct data for the last poster.

 

I'm really in need of some help, if you need any more info just let me know.

 

Cheers!

Link to comment
Share on other sites

sorry, I missed out the where clause!

 

should be:

 

WHERE t.forumid = $forumid

 

I'm getting all threads/posts that are in a certain forum (e.g. $forumid = 2).

 

There is what i'd like to do:

Step 1: Get all threads from forumthread (there is no postdate here, so I cant order by postdate to get them in the correct order

Step 2: Join forumpost on forumthread.threadid = forumpost.threadid, then order all of the data on forumpost.postdate so that the thread with the latest post is at the top, then in order of last postdate (DESC)

 

That should make it more clear, thank you for your reply!

Link to comment
Share on other sites

I have 3 rows in forumthread - therefore I've made three posts.

I have 7 rows in forumpost - therefore I have made 4 replies (7-3).

 

Nope, that's not working - I changed some parts of the query but it still returns 1 row, and it does not order it correctly.

 

What I was trying to do was to group by threadid then order that group on postdate DESC.

Link to comment
Share on other sites

Thanks for your reply.

 

I've done that, and it does work/order correctly, however, it's listing every post. I only need it to list each thread, so there should be three results with the latest post selected from forumpost.

Link to comment
Share on other sites

That doesn't order, you must have missed ORDER BY - also, it still lists all 7 of the posts, not just the three i'm looking for. I'm sure there should be a group by somewhere.. Hmm - this is really annoying me.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.