我的数据库中有一个表格如下:
m | v
-------
1 | 10
1 | 15
1 | 15
2 | 8
2 | 12
2 | 14
3 | 25
3 | 15
3 | 18
4 | 12
我希望按v
选择最多总和m
组,并在第一步中使用此代码创建表格:
select m,sum(v) as v from table group by m
m | v
-------
1 | 35
2 | 34
3 | 54
4 | 12
并且对于此表中的select max,我的代码为:
select max(v) as v,m from
(select sum(v) as v,m from table group by `m`)ta
v | m
------
54 | 1
v | m
------
54 | 3
我没有任何解决这个问题的想法。
答案 0 :(得分:2)
试试这个:
select m,sum(v) as v from table group by m ORDER BY v DESC LIMIT 0,1
答案 1 :(得分:1)
尝试此查询
SELECT v,m from
(SELECT SUM(v) as v, m FROM maxValues GROUP BY `m`) ta
ORDER BY v DESC
LIMIT 1
答案 2 :(得分:0)
试试这个
(select max(sum(v)), m as v,m from table group by `m`)