SQL select in select where from result max

时间:2014-08-21 11:42:39

标签: mysql

我有一个名为的作品的表格,列名为: group_id,名称,金钱

enter image description here

我的查询选择group_id并加钱如下:

select group_id, sum(money) as time FROM works group by group_id

此查询产生以下结果

enter image description here

现在我需要补充查询以查找最大金额和组ID。我想得到以下结果:

group id: 2, money: 320 

3 个答案:

答案 0 :(得分:0)

如果我理解正确,您可以通过金钱订购并选择第一行:

select group_id, sum(money) as time
from works
group by group_id
order by sum(money) desc
limit 1;

答案 1 :(得分:0)

试试这个

select group_id, sum(money) as money as time FROM works group by group_id having max(money)

答案 2 :(得分:0)

select g.group_name, sum(money) as time
from works w
INNER JOIN group g on g.group_id = w.group_id
group by w.group_id
order by sum(money) desc
limit 1;