簡體   English   中英

匯總mysql中的所有重復列

[英]sum all repetitious column in mysql

你好,我在 mysql 中有一個表,當另一列中有重復數據時,我想從該表中匯總多條記錄。 例如我有這個數據表:


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

我想創建這樣的輸出代碼

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

讓我有另一張這樣的桌子

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

我也想這樣展示

1 | 13 | 200 // sum all way and all deposit that one abutment_user_id
4 | 15 | 183
5 | 17 | 115

你似乎想要聚合:

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

旁注:在列名前面加上表名是沒有意義的:這會使列名更長,沒有任何明顯的好處: register_user_course_id應該被稱為idregister_user_course_price應該只是price ,等等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM