[英]sum all repetitious column in mysql
hello i have a table in mysql and i want to sum a multi records from this table when have repetitious data in another column.你好,我在 mysql 中有一个表,当另一列中有重复数据时,我想从该表中汇总多条记录。 for example i have this table with this data:
例如我有这个数据表:
table name = register_user_course
columns = register_user_course_id, register_user_register_id, register_user_course_name, register_user_course_price
data like =
1 | 3 | 3dmax | 300
2 | 3 | photoshop | 500
4 | 4 | 3dmax | 300
5 | 6 | icdl | 500
6 | 4 | icdl | 900
7 | 9 | accounting | 1000
and i want to create to code that output like this我想创建这样的输出代码
1 | 3 | 800 // sum all register_course_price with the same register_user_register_name on aoutput one records
4 | 4 | 1200 // sum all register_course_price with the same register_user_register_name on aoutput one records
5 | 6 | 500
7 | 9 | 1000
lets I have another table like this让我有另一张这样的桌子
table name = deposit
columns = deposit_id, deposit_abutment_id, deposit_price
table data like=
1 | 13 | 0,10,50,0,0 // every comma sign has one way to get money
2 | 13 | 10,30,0,0,0
3 | 13 | 100,0,0,0,0
4 | 15 | 50,0,0,0,50
5 | 17 | 100,10,5,0,0
6 | 15 | 33,50,0,0,0
and i want to also show like this我也想这样展示
1 | 13 | 200 // sum all way and all deposit that one abutment_user_id
4 | 15 | 183
5 | 17 | 115
You seem to want aggregation:你似乎想要聚合:
select
min(register_user_course_id) register_user_course_id,
register_user_register_id,
sum(register_user_course_price) register_user_course_price
from register_user_course
group by register_user_register_id
Side note: there is no point prefixing the column names with the table name: this makes the column names much longer, without any obvious benefit: register_user_course_id
should be called id
, register_user_course_price
should be just price
, and so on.旁注:在列名前面加上表名是没有意义的:这会使列名更长,没有任何明显的好处:
register_user_course_id
应该被称为id
, register_user_course_price
应该只是price
,等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.