MySQL 分组依据和其他列的总和值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15048887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL Group By and Sum total value of other column
提问by Joe
I have 2 columns like this:
我有两列这样的:
+----------+--------+
| word | amount |
+----------+--------+
| dog | 1 |
| dog | 5 |
| elephant | 2 |
+----------+--------+
I want to sum the amounts, to get the result
我想对金额求和,以获得结果
+----------+--------+
| dog | 6 |
| elephant | 2 |
+----------+--------+
What I have tried so far (and failed) is this:
到目前为止我尝试过(但失败了)是这样的:
SELECT word, SUM(amount) FROM `Data` Group By 'word'
回答by John Woo
Remove the single quote around the WORD. It causes the column name to be converted as string.
删除WORD.周围的单引号。它导致列名被转换为字符串。
SELECT word, SUM(amount)
FROM Data
Group By word
回答by Chittaranjan Sethi
It should be grave accentsymbol not single quote:
SELECT word, SUM( amount )
FROM Data
GROUP BY `word`;
Output:
输出:
word SUM(amount)
dog 6
Elephant 2


回答by Manish Sahu
SELECT word, SUM(amount) FROM Data Group By word;
回答by peter
Try this unconventional approach.
试试这种非常规的方法。
SELECT word, SUM(amount)
FROM Data
Group By word
回答by Sani Kamal
SELECT column_name1, SUM(column_name2)
FROM table_name
GROUP BY column_name1

