Estoy consultando desde una tabla con el siguiente formato:
id|provider|score -------------------------------- 1 | att | '{"attscore":300}' 1 | verizon | '{"verizonscore":299}' 2 | att | '{"attscore":200}' 3 | verizon | '{"verizonscore":155}'Estoy tratando de obtener una tabla que se parece a la siguiente:
id|attscore|verizonscore ------------------------- 1 | 300 | 299 2 | 200 | null 3 | null | 155Tenga en cuenta que solía json en sql
CREATE TABLE table1 ( `id` INTEGER, `provider` VARCHAR(7), `score` VARCHAR(22) ); INSERT INTO table1 (`id`, `provider`, `score`) VALUES ('1', 'att', '{"attscore":300}'), ('1', 'verizon', '{"verizonscore":299}'), ('2', 'att', '{"attscore":200}'), ('3', 'verizon', '{"verizonscore":155}');
SELECT id, GROUP_CONCAT(CASE WHEN provider = 'att' THEN `score`->"$.attscore" ELSe NULL END) attscore ,GROUP_CONCAT(CASE WHEN provider = 'verizon' THEN `score`->"$.verizonscore" ELSe NULL END) verizonscore FROM table1 GROUP BY ididentificación | puntuación | verizonscore -: | :------- | :----------- 1 | 300 | 299 2 | 200 | nulo 3 | nulo | 155
db<>violín aquí
Esto funciona bastante bien con un número fijo de columnas, si tiene muchas más, debe hacer algo como esto