Jump to content

Ordering results based on another table


jamiesage123

Recommended Posts

Hello all,

 

I've been trying to create a query which returns the newest results based on another table row.

 

Basically, i'm trying to create a small forums, where the topic with the newest reply is always at the top.

 

Here is what my tables look like:

 

(I have renamed a few rows, just so it isn't the same as my database)

 

forums

 

id | oid | author | title | detail | views | Tdate | replies

 

(oid is a persons group; they have to have the same oid to see a particular forum/topic)

(Tdate is a 'CURRENT_TIMESTAMP')

 

 

forums_replies

 

rid | topicid | oid | author | message | date

 

(date is a unix timestamp)

 

At the moment, i've tried using a few queries which haven't worked. The closest i've gotten is this:

 

SELECT `forums`. * , `forums_replies`.`date`, `forums_replies`.`orgid`, `forums_replies`.`topicid`
FROM `forums`
INNER JOIN `forums_replies` ON `forums_replies`.`oid` = `forums`.`oid`
AND `forums`.`id` = `forums_replies`.`topicid`
GROUP BY `forums`.`id`
ORDER BY  `forums_replies`.`date` DESC

 

It does work when they is only a few results in the forums_replies table. However, it seems like it gets mixed up when they is over 2 results in their, causing it not to be ordered in the correct way. It also shows duplicated results.

 

Any help will be really appreciated.  :D

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.