假设我有这个问题

 SELECT ft.*, m.*
   FROM forum_topics ft
     INNER JOIN members m ON ft.author = m.id
     WHERE ft.forum =  '$forum'
     ORDER BY ft.lastpost DESC

我还想从表forum_replies中获取行计数,其中id = ft.id
我怎么能那样做?

最佳答案

根据您的数据库实现,这可能会起作用:

 SELECT ft.*,
        m.*,
        ( SELECT count(1)
            FROM forum_replies fr
           WHERE fr.id = ft.id) AS nr_of_replies
   FROM forum_topics ft
     INNER JOIN members m ON ft.author = m.id
     WHERE ft.forum =  '$forum'
     ORDER BY ft.lastpost DESC

高温高压

关于mysql - sql联接和计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8762399/

10-12 04:30