46

我有 2 列是这样的:

+----------+--------+
|   word   | amount |
+----------+--------+
| dog      |      1 |
| dog      |      5 |
| elephant |      2 |
+----------+--------+

我想总结金额,得到结果

+----------+--------+
| dog      |      6 |
| elephant |      2 |
+----------+--------+

到目前为止,我尝试过(但失败了)是这样的:

SELECT word, SUM(amount) FROM `Data` Group By 'word'
4

5 回答 5

76

删除WORD. 它导致列名被转换为字符串。

SELECT word, SUM(amount) 
FROM Data 
Group By word
于 2013-02-24T05:08:57.427 回答
15

它应该是重音符号而不是单引号

SELECT word, SUM( amount )
FROM Data
GROUP BY `word`;

输出:

word     SUM(amount)
dog           6
Elephant      2

在此处输入图像描述

于 2013-02-24T05:25:03.037 回答
3
SELECT word, SUM(amount) FROM Data Group By word;
于 2013-02-24T06:44:03.053 回答
2

试试这种非常规的方法。

SELECT word, SUM(amount) 
FROM Data 
Group By word
于 2014-04-07T07:33:38.143 回答
1
SELECT column_name1, SUM(column_name2) 
FROM table_name 
GROUP BY column_name1
于 2019-08-28T11:45:02.200 回答