且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JOIN子句中的MYSQL子查询SELECT

更新时间:2022-11-25 22:05:43

很难看到表的DDL,相关的示例数据和所需的输出.

It's hard to tell without seeing DDL of your tables, relevant sample data and desired output.

我可能对您的要求有误,但是请尝试以下操作:

I could've got your requirements wrong, but try this:

SELECT *  
  FROM forum_cat c LEFT JOIN 
       (SELECT t.cat_id, 
               p.topic_id, 
               t.title, 
               p.id, 
               p.body, 
               MAX(p.`date`) AS `date`, 
               p.author_id, 
               u.username
          FROM forum_post p INNER JOIN
               forum_topic t ON t.id = p.topic_id INNER JOIN
               `user` u ON u.user_id = p.author_id
         GROUP BY t.cat_id) d ON d.cat_id = c.id
 WHERE c.main_cat = 1
 ORDER BY c.list_no