mysql按行排序

js5cn81o  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(283)

我想从我的电脑里提取数据 forum_topics 以及 forum_replies 获取最后答复日期的表格;

SELECT 
    (SELECT date FROM forum_replies WHERE topic=5 AND date < NOW()
    ORDER BY id DESC LIMIT 1) as lastreply, 
    ft.* FROM forum_topics ft
    ORDER by lastreply DESC LIMIT 5

它得到了 lastreply 记录集中的数据,但不使用它对记录集中的行进行排序。
我知道为什么,但不知道怎么修?

nxowjjhe

nxowjjhe1#

应使用相关子查询:

SELECT 
    (SELECT r.date FROM forum_replies r WHERE ft.topic = r.topic
    ORDER BY r.id DESC LIMIT 1) as lastreply, 
    ft.* 
FROM forum_topics ft
ORDER by lastreply DESC LIMIT 5

相关问题