gpt4 book ai didi

MySQL - "Most social User"在多个表中评论最多

转载 作者:行者123 更新时间:2023-11-29 03:45:38 26 4
gpt4 key购买 nike

我有 2 个关注表 - 'videoComments'、'storyComments'。

我需要在 videoCommentsstoryComments 中找到条目最多的“posterID”。这是我目前的代码,但它只调用 videoComments:

$sql = "SELECT (SELECT posterID 
FROM videoComments
GROUP BY posterID
ORDER BY COUNT(posterID) DESC LIMIT 1) ) AS mostSocialUser ";

我如何提取它并比较两个表中 posterID 的数量?

最佳答案

使用:

   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

关于MySQL - "Most social User"在多个表中评论最多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5958212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com