MySQL:如何总结行的值并对结果进行排序? [英] MySQL: how to sum up vaues of rows and sort the result?

查看:117
本文介绍了MySQL:如何总结行的值并对结果进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个SQL语句,该语句总结一些特定的值并对结果进行排序.更清楚地说:我有一个包含标识符和值的表:

I'm looking for a SQL-statement that sums up some specific values and sorts the result. To be more clear: I have a table that consists of identifiers and values:

id     val
ab     10
ab     12
ab      3
cd     25
cd     10
ef      2
ef      7

这里必须对ab,cd和ef的所有值求和并按结果排序,以便得到以下内容:

Here all values for ab, cd and ef have to be summed up and ordered by result so that I get the following:

cd     35
ab     25
ef      9

那么是否有一个SQL语句可以一次性执行该任务?

So is there a SQL-statement that performs that task in one go?

推荐答案

SELECT id, SUM(val) as total
FROM your_table
GROUP BY id
ORDER BY total DESC;

这篇关于MySQL:如何总结行的值并对结果进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆