我知道这很简单,但它让我疯狂......
我有一个用户表,评论表和图片表.
我想根据提交的内容排名前10位用户(他们的评论总数和提交的照片).
而已.
惭愧我
更新:基于Ed的回答.
这是我的设置:
users表(user_id,用户名)
图像表(img_id,submittedby_id = users.user_id)
注释表(id,submittedby_id = users.user_id)
和最后的查询:
select submittedby_id, sum(total) from (select submittedby_id, count(img_id) as total from images group by submittedby_id union select submittedby_id, count(id) as total from comments group by submittedby_id ) as x group by submittedby_id order by sum(total) desc limit 10;
Ed Carrel.. 5
也许有点像这样:
select username, sum(submissions) from (select username, count(picture_id) from pictures group by username union select username, count(comment_id) from comments group by username ) group by username order by sum(submissions) desc limit 10;
概念性概述:
计算每个表中用户的提交
联合那些,因此每个用户将从子查询中获得0到2个计数.
再组一次,将两个计数相加,然后订购,以便最高金额在最高位置.
希望这可以帮助.
也许有点像这样:
select username, sum(submissions) from (select username, count(picture_id) from pictures group by username union select username, count(comment_id) from comments group by username ) group by username order by sum(submissions) desc limit 10;
概念性概述:
计算每个表中用户的提交
联合那些,因此每个用户将从子查询中获得0到2个计数.
再组一次,将两个计数相加,然后订购,以便最高金额在最高位置.
希望这可以帮助.