一个表中的Mysql行被移动并合并到命名列中



我有以下数据

drop table if exists states;
create table states (id integer, state varchar(100), content varchar(100));
insert into states (id, state, content) values (1, 'soc', '73');
insert into states (id, state, content) values (2, 'range', '412');
insert into states (id, state, content) values (3, 'range', '410');
insert into states (id, state, content) values (3, 'soc', '71');
insert into states (id, state, content) values (5, 'range', '405');

使用以下语句

SELECT id,
CASE WHEN state = 'range' THEN content ELSE null END AS 'range',
CASE WHEN state = 'soc' THEN content ELSE null END AS 'soc'
FROM states;

结果是

id范围soc
173
2412
3410
371
5405
SELECT id,
CASE WHEN state = 'range' THEN content ELSE null END AS 'range',
CASE WHEN state = 'soc' THEN content ELSE null END AS 'soc'
FROM states;
>3//tr>1//tr>
idrangesoc
1null
2412null
3410null
3
5405null

最新更新