我有两张关注的表 - “videoComments”,“storyComments”。
我需要找到videoComments
和storyComments
中条目最多的'posterID'。这是我到目前为止的代码,但它只调用videoComments
:
$sql = "SELECT (SELECT posterID
FROM videoComments
GROUP BY posterID
ORDER BY COUNT(posterID) DESC LIMIT 1) ) AS mostSocialUser ";
如何提取它并比较两个表中的posterID COUNT?
答案 0 :(得分:0)
试试这个:
SELECT (
SELECT posterID FROM (
SELECT posterID FROM videoComments
UNION
SELECT posterID FROM storyComments
) GROUP BY posterID
ORDER BY COUNT(posterID) DESC LIMIT 1
) AS mostSocialUser
答案 1 :(得分:0)
使用:
SELECT x.posterid,
COUNT(y.posterid) + COUNT(z.posterid) AS numComments
FROM (SELECT vc.posterid
FROM VIDEOCOMMENTS vc
UNION
SELECT sc.posterid
FROM STORYCOMMENTS sc) x
LEFT JOIN VIDEOCOMMENTS y ON y.posterid = x.posterid
LEFT JOIN STORYCOMMENTS z ON z.posterid = x.posterid
GROUP BY x.posterid
ORDER BY numComments DESC
LIMIT 1