mysql行转列

select 0 AS id,mac, file_md5,substring_index(substring_index(a.type_str,',',b.help_topic_id+1),',',-1) as type_val
from 
(
SELECT
mac,
file_md5,
concat_ws(',',`1`,`2`,`3`,`4`,`5`,`6`,`7`,`8`,`9`,`10`,`11`,`12`,`13`,`14`,`15`,`16`,`17`,`18`,`19`,`20`,`21`,`22`,`23`,`24`,`25`,`26`,`27`,`28`,`29`,`30`,`31`,`32`,`33`,`34`,`35`,`36`,`37`,`38`,`39`,`40`,`41`,`42`,`43`,`44`,`45`,`46`,`47`,`48`,`49`,`50`) as type_str
FROM
directory_excel_dataType_rmInconsistent 
)
AS a
Left join
-- join 顺序会错乱
mysql.help_topic b
on b.help_topic_id < (length(a.type_str) - length(replace(a.type_str,',',''))+1);

亲测:使用join时列的顺序会错乱,即第一列和其他列的顺序会打乱,left join顺序不会错乱

原文地址:https://www.cnblogs.com/qfdy123/p/14630756.html