Dwza
Dwza

Reputation: 6565

MySQL select sum as given column

Lets say I have a Table with 3 columns

|aaa | bbb | ccc|
|---------------|
|111 | 123 | uuu|
|333 | 234 | uuu|
|555 | 345 | nnn|

Now I select a sum like:

SELECT *, sum(bbb) as bbb from myTable GROUP BY ccc

I recive

|aaa | bbb | ccc | bbb|
|---------------------|
|333 | 234 | uuu | 357|
|555 | 345 | nnn | 345|

bbb is set new in output...

Is there a way to replace the column that exists so I get:

|aaa | bbb | ccc |
|----------------|
|333 | 357 | uuu |
|555 | 345 | nnn |

I know I could use an other name but using an other name is not the question :)

Upvotes: 0

Views: 419

Answers (2)

stwalkerster
stwalkerster

Reputation: 1828

Don't include that column in the SELECT part of the query.

SELECT aaa, sum(bbb) as bbb, ccc from myTable GROUP BY ccc;

Upvotes: 3

Jens
Jens

Reputation: 69515

You have to add all columns you need to the select clause:

SELECT aaa, sum(bbb) as bbb,ccc from myTable GROUP BY ccc

Upvotes: 3

Related Questions