这是我的表格:
CREATE TABLE sometbl ( ID INT, NAME VARCHAR(50) );
INSERT INTO sometbl VALUES (1, 'Smith'), (2, 'Julio|Jones|Falcons'), (3,
'White|Snow'), (4, 'Paint|It|Red'), (5, 'Green|Lantern'), (6, 'Brown|bag');
表格输出
----------------------------------------
ID NAME
1 Smith
2 Julio|Jones|Falcons
3 White|Snow
4 Paint|It|Red
5 Green|Lantern
6 Brown|bag
------------------------------------------
输出应该如下所示:
对于 (2),示例行看起来像 >> “3, white”, “3, Snow” ...
请帮助我。
最佳答案
-- sample data = 2 One|Two|Three|Four
-- get the first bit: 2 One
SELECT ID, substring_index(NAME, '|', 1) AS NAME FROM tab
UNION
-- get the 2nd bit: 2 Two
SELECT ID, substring_index(substring_index(NAME, '|', 2),'|',-1) AS NAME FROM tab
UNION
-- get the 3rd bit: 2 Three
SELECT ID, substring_index(substring_index(NAME, '|', 3),'|',-1) AS NAME FROM tab
UNION
-- get the last bit: 2 Four
SELECT ID, substring_index(NAME, '|',-1) AS NAME FROM tab
关于mysql - 在 MySQL 中编写一个过程,使用分隔符将列拆分为行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42128957/