Home » Php » php – sum all repetitious column in mysql

php – sum all repetitious column in mysql

Posted by: admin February 25, 2020 Leave a comment

Questions:

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.
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
How to&Answers:

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.