MySql获取新列的每一行中的列的总和

时间:2017-01-28 14:13:35

标签: mysql

我有以下数据集:

market sale
fr       10
uk       10
fr       12
fr        3
uk        1

我想添加一个新列,我可以在这里显示新列的每一行中的销售总和

market sale total sale 
fr       10         38 
uk       10         38 
us        3         38
fr        2         38
fr       12         38
uk        1         38

mu查询需要在Mysql中

感谢

1 个答案:

答案 0 :(得分:1)

您请求选择ID设计错误,但如果您确实需要

您可以使用子选择

计算列中的总和
SELECT  market, sale, (select sum(sale) as total from my_table) as  total
from my_table 

如果你只需要一些国家,例如FR,UK,你可以

SELECT  market, sale, (select sum(sale) as total 
                           from my_table
                           where market in ('FR', 'UK')) as  total
from my_table 
here market in ('FR', 'UK')

或者你总是需要总和

SELECT  market, sale, (select sum(sale) as total 
                           from my_table
                          ) as  total
from my_table 
here market in ('FR', UK')