Note that there are some explanatory texts on larger screens.

plurals
  1. POSort by largest of two date values from two tables
    primarykey
    data
    text
    <p>I am working on a simple forum and am struggling when trying to sort the topics when listed - if the topics all have replies then it works fine, if none have replies then it works fine but if they have combination of both then it doesn't work properly. I need to take the largest value out of forum_topics and forum_replies and sort using that value.</p> <p>I have looked up greatest and it seems to be what I need but it doesn't work for me:</p> <pre><code>SELECT GREATEST(MAX(forum_topics.topic_date),MAX(forum_replies.reply_date)) as g_date from forum_topics left join forum_replies on forum_topics.topic_id=forum_replies.reply_topic where forum_topics.topic_category='$category_id' GROUP BY forum_topics.topic_id ORDER BY g_date desc </code></pre> <p>forum_topics</p> <pre><code>topic_id topic_date topic_category 1 26/12/2012 23:09 2 2 26/12/2012 10:13 5 3 27/12/2012 02:04 2 4 27/12/2012 02:25 7 5 27/12/2012 02:32 3 6 27/12/2012 02:46 2 8 27/12/2012 02:49 2 9 27/12/2012 16:05 2 10 06/01/2013 19:57 2 11 06/01/2013 20:35 2 12 06/01/2013 20:36 5 </code></pre> <p>forum_replies</p> <pre><code>reply_id reply_date reply_topic 1 29/12/2012 19:14 1 2 26/12/2012 23:49 3 3 31/12/2012 00:00 1 4 27/12/2012 02:03 1 5 27/12/2012 02:15 3 6 27/12/2012 02:16 3 7 27/12/2012 02:17 3 8 27/12/2012 02:26 4 9 27/12/2012 03:19 3 10 27/12/2012 03:26 3 11 27/12/2012 03:29 3 12 27/12/2012 03:29 3 13 27/12/2012 03:30 3 14 27/12/2012 03:32 3 15 27/12/2012 03:35 3 17 27/12/2012 15:23 8 18 06/01/2013 20:35 2 19 06/01/2013 20:38 1 </code></pre> <p>Desire outcome for category id=2:</p> <pre><code>1 06/01/2013 20:38 (reply 19) 11 06/01/2013 20:35 (topic 11) 10 06/01/2013 19:57 (topic 10) 9 27/12/2012 16:05 (topic 9) 3 27/12/2012 03:35 (reply 15) 6 27/12/2012 02:46 (topic 6) </code></pre> <p>Actual output</p> <pre><code>1 06/01/2013 20:38:23 3 27/12/2012 03:35:33 6 27/12/2012 02:46:57 9 27/12/2012 16:05:42 10 06/01/2013 19:57:33 11 06/01/2013 20:35:48 </code></pre> <p>What am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload