说我有这个查询
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
获取行数。
我怎么能这样做?
答案 0 :(得分:2)
根据您的数据库实施情况,这可能有效:
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
HTH
答案 1 :(得分:0)
SELECT ft.*, m.*, fr.ReplyCount
FROM forum_topics ft
INNER JOIN members m ON ft.author = m.id
inner join (
Select Id, Count(*) as ReplyCount
from forum_replys
group by id
) as fr
on ft.Id = fr.Id
WHERE ft.forum = '$forum'
ORDER BY ft.lastpost DESC