这是我的表:
id user_id type value
1 1 type1 2
2 2 type1 1
3 1 type2 5
4 1 type1 2
我想要这样的输出:
user_id type1 type2
1 4 5
2 1 0
请帮助
答案 0 :(得分:2)
您可以尝试这样:
SELECT
user_id,
sum( if( type = 'type1', value, 0 ) ) AS type1,
sum( if( type = 'type2', value, 0 ) ) AS type2
FROM
table_name
GROUP BY
user_id;
答案 1 :(得分:1)
我想,这就是你要找的东西:
select
user_id,
(select sum(value) from <yourtablename> sub1 where type = 'type1' where sub1.user_id = baseTable.user_id) as type1,
(select sum(value) from <yourtablename> sub2 where type = 'type2' where sub2.user_id = baseTable.user_id) as type2,
from
<yourtablename> baseTable