MySQL将多个值拆分为多行

时间:2018-09-03 08:50:32

标签: mysql

我需要一个将多个列中的多个值拆分为另一列的帮助。下面是一个示例

CREATE TABLE split
(
    `Col_1`             VARCHAR(120),
    `Col_2`                 VARCHAR(50),
    `Col_3`                VARCHAR(20),
    `Col_4`                 VARCHAR(50)
);

Insert into split (Col_1,Col_2,Col_3,Col_4) values ('ABC','1','10',null);
Insert into split (Col_1,Col_2,Col_3,Col_4) values ('DEF','2,3','30,40',null);
Insert into split (Col_1,Col_2,Col_3,Col_4) values ('GHI','4,5','50','500,600,700');

select * from split;

+-------+-------+-------+-------------+
| Col_1 | Col_2 | Col_3 | Col_4       |
+-------+-------+-------+-------------+
| ABC   | 1     | 10    | NULL        |
| DEF   | 2,3   | 30,40 | NULL        |
| GHI   | 4,5   | 50    | 500,600,700 |
+-------+-------+-------+-------------+

我不是这方面的专家,但是一直在玩耍,并且设法仅将col_2分成多个行,如下所示:

SELECT
  Col_1,Col_2,Col_3,Col_4,
  SUBSTRING_INDEX(SUBSTRING_INDEX(split.Col_2, ',', numbers.n), ',', -1) Col_2_NEW,
  SUBSTRING_INDEX(SUBSTRING_INDEX(split.Col_3, ',', numbers.n), ',', -1) Col_3_NEW,
  SUBSTRING_INDEX(SUBSTRING_INDEX(split.Col_4, ',', numbers.n), ',', -1) Col_4_NEW
FROM
  (SELECT 1 n UNION ALL SELECT 2
   UNION ALL SELECT 3 UNION ALL SELECT 4) numbers INNER JOIN split
  ON CHAR_LENGTH(split.Col_2) - CHAR_LENGTH(REPLACE(split.Col_2, ',', ''))>=numbers.n-1
ORDER BY Col_2, n;

+-------+-------+-------+-------------+-----------+-----------+-----------+
| Col_1 | Col_2 | Col_3 | Col_4       | Col_2_NEW | Col_3_NEW | Col_4_NEW |
+-------+-------+-------+-------------+-----------+-----------+-----------+
| ABC   | 1     | 10    | NULL        | 1         | 10        | NULL      |
| DEF   | 2,3   | 30,40 | NULL        | 2         | 30        | NULL      |
| DEF   | 2,3   | 30,40 | NULL        | 3         | 40        | NULL      |
| GHI   | 4,5   | 50    | 500,600,700 | 4         | 50        | 500       |
| GHI   | 4,5   | 50    | 500,600,700 | 5         | 50        | 600       |
+-------+-------+-------+-------------+-----------+-----------+-----------+

但是,我也想将col_3和col_4拆分为new,因此它的输出如下。

+-------+-------+-------+-------------+-----------+-----------+-----------+
| Col_1 | Col_2 | Col_3 | Col_4       | Col_2_NEW | Col_3_NEW | Col_4_NEW |
+-------+-------+-------+-------------+-----------+-----------+-----------+
| ABC   | 1     | 10    | NULL        | 1         | 10        | NULL      |
| DEF   | 2,3   | 30,40 | NULL        | 2         | 30        | NULL      |
| DEF   | 2,3   | 30,40 | NULL        | 2         | 40        | NULL      |
| DEF   | 2,3   | 30,40 | NULL        | 3         | 30        | NULL      |
| DEF   | 2,3   | 30,40 | NULL        | 3         | 40        | NULL      |
| GHI   | 4,5   | 50    | 500,600,700 | 4         | 50        | 500       |
| GHI   | 4,5   | 50    | 500,600,700 | 4         | 50        | 600       |
| GHI   | 4,5   | 50    | 500,600,700 | 4         | 50        | 700       |
| GHI   | 4,5   | 50    | 500,600,700 | 5         | 50        | 500       |
| GHI   | 4,5   | 50    | 500,600,700 | 5         | 50        | 600       |
| GHI   | 4,5   | 50    | 500,600,700 | 5         | 50        | 700       |
+-------+-------+-------+-------------+-----------+-----------+-----------+

到目前为止,我已经进行了搜索,他们只将行分成一列,却找不到类似于我的问题。 也许缺少一些联接或某些联合,我不知道,因为我不擅长查询。 有人能帮我一下吗?而不要求我阅读指南或手册:-)

预先感谢

2 个答案:

答案 0 :(得分:1)

您可以尝试推荐in this thread here之一。 类似于

 SELECT s.[Col_1], Split.a.value('.', 'VARCHAR(100)') AS String  
 FROM  (SELECT [Col_1], 
        CAST ('<M>' + REPLACE([Col_2], ',', '</M><M>') + '</M>' AS XML) AS String  
        FROM  split) AS s
 CROSS APPLY String.nodes ('/M') AS Split(a);

在您的列上进行迭代应该可以正常工作。

编辑:对不起,这不是MySQL。可行的解决方案请参见下文。

以下代码应适用于前两列。

1。)创建表:

CREATE TABLE split(
    `Col_1`    VARCHAR(120),
    `Col_2`    VARCHAR(50),
    `Col_3`    VARCHAR(20),
    `Col_4`    VARCHAR(50)
);

INSERT INTO split (Col_1,Col_2,Col_3,Col_4) values ('ABC','1','10',null);
INSERT INTO split (Col_1,Col_2,Col_3,Col_4) values ('DEF','2,3','30,40',null);
INSERT INTO split (Col_1,Col_2,Col_3,Col_4) values ('GHI','4,5','50','500,600,700');

这导致

SELECT * FROM split;

Col_1       Col_2       Col_3       Col_4
ABC         1           10          (null)
DEF         2,3         30,40       (null)
GHI         4,5         50          500,600,700

2。)在Col_2中拆分字符串:

SELECT
  split.Col_1,
  SUBSTRING_INDEX(SUBSTRING_INDEX(split.Col_2, ',', numbers.n), ',', -1) Col_2,
  Col_3,
  Col_4
FROM
  (select 1 n UNION ALL
  select 2 UNION ALL select 3 UNION ALL
  select 4 UNION ALL select 5) numbers INNER JOIN split
  ON CHAR_LENGTH(split.Col_2)
 -CHAR_LENGTH(REPLACE(split.Col_2, ',', ''))>=numbers.n-1
ORDER BY Col_1, Col_2;

3。)结果:

Col_1       Col_2       Col_3       Col_4
ABC         1           10          (null)
DEF         2           30,40       (null)
DEF         3           30,40       (null)
GHI         4           50          500,600,700
GHI         5           50          500,600,700

这是一个带有以上代码的SQL提琴:http://sqlfiddle.com/#!9/948fcb/4。 您应该可以从那里进行迭代。如果您需要更多指导,只需评论此帖子。

重要警告:在一列中最多可以使用5个逗号分隔的字符串。

解决方案的灵感来自fthiella对SQL split values to multiple rows的回答。

答案 1 :(得分:0)

只想回答最终查询,这将根据需要提供所需的输出。 将其张贴在这里,也许会帮助其他人。

SELECT distinct Col_1,Col_2,Col_3,Col_4,
 SUBSTRING_INDEX(SUBSTRING_INDEX(t.Col_2, ',', n.n), ',', -1) Col_2_New,
 SUBSTRING_INDEX(SUBSTRING_INDEX(t.Col_3, ',', n1.n), ',', -1) Col_3_New,
 SUBSTRING_INDEX(SUBSTRING_INDEX(t.Col_4, ',', n2.n), ',', -1) Col_4_New
  FROM split t CROSS JOIN 
  (
   SELECT a.N + b.N * 10 + 1 n
     FROM 
    (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ) a
   ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ) b
   ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ) c
    ORDER BY n 
   ) n,
  (
   SELECT a.N + b.N * 10 + 1 n
     FROM 
    (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ) a
   ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ) b
   ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ) c
    ORDER BY n 
   ) n1,
  (
   SELECT a.N + b.N * 10 + 1 n
     FROM 
    (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ) a
   ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ) b
   ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 ) c
    ORDER BY n 
   ) n2
WHERE coalesce(n.n,'0') <= 1 + (LENGTH(coalesce(t.Col_2,'0')) - LENGTH(REPLACE(coalesce(t.Col_2,'0'), ',', '')))
 AND coalesce(n1.n,'0') <= 1 + (LENGTH(coalesce(t.Col_3,'0')) - LENGTH(REPLACE(coalesce(t.Col_3,'0'), ',', '')))
 AND coalesce(n2.n,'0') <= 1 + (LENGTH(coalesce(t.Col_4,'0')) - LENGTH(REPLACE(coalesce(t.Col_4,'0'), ',', '')))
 ORDER BY 1,2,3,4,5,6,7 ;