table name : data
我的数据库表是:
id range number value
1 a no1 100
2 b no1 101
3 a no2 102
4 c no2 103
5 a no3 104
6 b no3 105
我需要输出
range no1 no2 no3
a 100 102 104
b 101 0 105
c 0 103 0
请有人帮我写mysql查询:(((
答案 0 :(得分:0)
您可以使用条件聚合执行此操作:
select range,
max(case when number = 'no1' then value else 0 end) as no1,
max(case when number = 'no2' then value else 0 end) as no2,
max(case when number = 'no3' then value else 0 end) as no3
from t
group by range;