我正在一个简单的论坛上工作,并且在尝试对列出的主题进行排序时遇到困难 - 如果主题都有回复,那么它工作正常,如果没有回复,那么它工作正常,但如果它们两者结合,那么它不会好好工作。我需要从 forum_topics 和 forum_replies 中取出最大值并使用该值进行排序。
我一直在寻找最好的东西,这似乎是我需要的,但它对我不起作用:
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
论坛主题
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
论坛回复
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
类别 id=2 的期望结果:
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)
实际输出
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
我究竟做错了什么?