mysql group by和其他列的总和

ao218c7q  于 2021-06-17  发布在  Mysql
关注(0)|答案(5)|浏览(329)

我有两个这样的专栏:

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

我想把这些金额相加,得到结果

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

到目前为止,我所尝试的(但失败的)是:

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

xlpyo6sf1#

试试这种非传统的方法。

SELECT word, SUM(amount) 
FROM Data 
Group By word
mm9b1k5b

mm9b1k5b2#

SELECT word, SUM(amount) FROM Data Group By word;
2lpgd968

2lpgd9683#

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

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

输出:

word     SUM(amount)
dog           6
Elephant      2

tuwxkamq

tuwxkamq4#

SELECT column_name1, SUM(column_name2) 
FROM table_name 
GROUP BY column_name1
xyhw6mcr

xyhw6mcr5#

删除 WORD . 它使列名转换为字符串。

SELECT word, SUM(amount) 
FROM Data 
Group By word

相关问题