当前位置:  开发笔记 > 编程语言 > 正文

按总提交选择用户

如何解决《按总提交选择用户》经验,为你挑选了1个好方法。

我知道这很简单,但它让我疯狂......

我有一个用户表,评论表和图片表.

我想根据提交的内容排名前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个计数.

    再组一次,将两个计数相加,然后订购,以便最高金额在最高位置.

希望这可以帮助.



1> Ed Carrel..:

也许有点像这样:

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个计数.

    再组一次,将两个计数相加,然后订购,以便最高金额在最高位置.

希望这可以帮助.

推荐阅读
有风吹过best
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有