Skip to content

Instantly share code, notes, and snippets.

@melotusme
Last active November 8, 2019 03:58
Show Gist options
  • Save melotusme/4c05c0a43489563ec23d9472464c5bbd to your computer and use it in GitHub Desktop.
Save melotusme/4c05c0a43489563ec23d9472464c5bbd to your computer and use it in GitHub Desktop.
sql mysql
-- mysql 行转列, 一个字段转成多行记录
-- 使用了辅助表 mysql.help_topic
SELECT
id, b.help_topic_id, SUBSTRING_INDEX( SUBSTRING_INDEX(id,',', b.help_topic_id+1),',',-1)
FROM
( SELECT "1,2,3,4,5" AS id ) a
JOIN mysql.help_topic b ON b.help_topic_id < ( LENGTH( a.id ) - LENGTH( REPLACE ( a.id, ',', '' ))+ 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment