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

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

本文介绍了MySql获取新列的每一行中的列的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据集:

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

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

and I want to add a new column where i can display the sum of sale in every row of the new column like this

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

mu 查询需要在 Mysql 中

mu query need to be in Mysql

谢谢

推荐答案

您请求的 select id 设计错误,但如果您确实需要

You request select id wrong designed but if you really need

您可以使用子选择计算列中的总和

You can calculate the sum in column using a subselect

SELECT  market, sale, (select sum(sale) as total from my_table) as  total
from my_table

如果您只需要某个国家/地区,例如法国、英国,您可以

if you need only some country eg FR, UK you can

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')

或者你总是需要你能得到的总和

or you need always the total sum you can

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

这篇关于MySql获取新列的每一行中的列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 00:26