0

I have this table:

user_id  date        total
1        2016-07-01  2
1        2016-08-01  4
2        2016-07-01  2
2        2016-08-01  5
3        2016-07-01  2
3        2016-08-01  3
4        2016-07-01  4
4        2016-08-01  3
5        2016-07-01  1
5        2016-08-01  5

and I want to transpose this table with this format:

userid  2016-07-01  2016-08-01
1       2           4
2       2           5
3       2           3
4       4           3
5       1           5

I'm using this query:

select 
user_id,
CASE WHEN MONTH(date)=7 then total ELSE 0 END as Jul,
CASE WHEN MONTH(date)=8 then total ELSE 0 END as Agust
from table_data
order by user_id

but the result is:

userid  2016-07-01  2016-08-01
1       2           0
1       0           4
2       2           0
2       0           5
3       2           0
3       0           3
4       4           0
4       0           3
5       1           0
5       0           5

How this query, I want to display to web using PHP.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
tomy
  • 1
  • 1

0 Answers0