这基本上是一个PIVOT
(您没有指定 RDBMS)我假设 MySQL 并且它没有PIVOT
函数,因此您需要使用带有CASE
语句的聚合函数来复制它。此解决方案rownumber
向每一行添加一个,以便您可以确定需要将多少name
个值转换为列。
如果您知道将拥有多少name
个值,则可以对这些值进行硬编码:
select plantid,
max(case when nameRn = 'name1' then name end) Name1,
max(case when nameRn = 'name2' then name end) Name2,
max(case when nameRn = 'name3' then name end) Name3
from
(
select plantid, name,
concat('name', @num := if(@plantid = `plantid`, @num + 1, 1)) as nameRn,
@plantid := `plantid` as dummy
from
(
select plantid, name, @rn:=@rn+1 overall_row_num
from yourtable, (SELECT @rn:=0) r
) x
order by plantid, overall_row_num
) src
group by plantid;
请参阅带有演示的 SQL Fiddle
如果您有未知数量的值,那么您可以使用准备好的语句来生成它的动态版本:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when nameRn = ''',
nameRn,
''' then name end) AS ',
nameRn
)
) INTO @sql
FROM
(
select plantid, name,
concat('name', @num := if(@plantid = `plantid`, @num + 1, 1)) as nameRn,
@plantid := `plantid` as dummy
from
(
select plantid, name, @rn:=@rn+1 overall_row_num
from yourtable, (SELECT @rn:=0) r
) x
order by plantid, overall_row_num
) src;
SET @sql = CONCAT('SELECT plantid, ', @sql, '
FROM
(
select plantid, name,
concat(''name'', @num := if(@plantid = `plantid`, @num + 1, 1)) as nameRn,
@plantid := `plantid` as dummy
from
(
select plantid, name, @rn:=@rn+1 overall_row_num
from yourtable, (SELECT @rn:=0) r
) x
order by plantid, overall_row_num
) src
GROUP BY plantid');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
请参阅带有演示的 SQL Fiddle
两者都会产生相同的结果:
| PLANTID | NAME1 | NAME2 | NAME3 |
-------------------------------------
| 1 | tree | rose | (null) |
| 2 | bush | (null) | (null) |
| 3 | tree | bush | rose |