Simple Query Question

Hello MySQL Gurus,

I have a simple query question.

For the sake of simplicity, let’s imagine this is my table of results.

[TABLE=“class: grid, width: 500, align: left”]
[TR]
[TD]user_id[/TD]
[TD]amount[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]24[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]32[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]55[/TD]
[/TR]
[/TABLE]

When I output this data using a while or foreach loop, how can I merge the rows that have the same user_id and also add it’s amount to one value. So it displays something like this:

[TABLE=“width: 500”]
[TR]
[TD]1[/TD]
[TD]Total amount: 44[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Total amount: 24[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Total amount: 55[/TD]
[/TR]
[/TABLE]

Thank you :slight_smile:

use SUM() and GROUP BY

:slight_smile: