Fahim Parkar
Fahim Parkar

Reputation: 31633

showing data in one row (from multiple rows)

Below is what I have

++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 1  +  1     +        +
+ 1  +        +   1    +
+ 2  +  1     +        +
+ 2  +        +   2    +
++++++++++++++++++++++++

What I want is

++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 1  +  1     +   1    +
+ 2  +  1     +   2    +
++++++++++++++++++++++++

I want to combine the rows and show data for user in one row against multiple rows like I have in table.

Any idea how to do it?

Note : I don't have any row who have data for all fields. And I don't have any user with below scenario.

++++++++++++++++++++++++
+ id + field1 + field2 +
++++++++++++++++++++++++
+ 3  +  1     +        +
+ 3  +  1     +   1    +
++++++++++++++++++++++++

Only 1 data in one row and two row per user.

I tried with

SELECT id, concat(field1), concat(field2) from myTable
GROUP BY id;

but its not coming.

data at sqlfiddle

Upvotes: 2

Views: 689

Answers (1)

eggyal
eggyal

Reputation: 125835

You need to use one of MySQL's aggregate functions when aggregating grouped data. Either use GROUP_CONCAT() in place of (the non-aggregate string function) CONCAT(), or else (better for numeric data) use SUM():

SELECT id, SUM(field1), SUM(field2) FROM myTable GROUP BY id

Demo

Upvotes: 3

Related Questions