MYSQL subquery SELECT in JOIN clause

前端 未结 1 1125
眼角桃花
眼角桃花 2021-01-11 12:48

Ok... well I have to put the subquery in a JOIN clause since it selects more than one column and putting it in the SELECT clause does not allow tha

相关标签:
1条回答
  • 2021-01-11 13:37

    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
    
    0 讨论(0)
提交回复
热议问题