mysql - 动态地将列拆分为行

时间:2017-09-19 12:23:08

标签: php mysql

我有表培训,我想将Training_name列值拆分为多行:

SLNO Category Training_name
1     A        1,5,9,15,12,16
2     B        2,6,10,17
3     C        1,3,7,19,14,18

我使用下面的查询,但使用此查询我只能分成两行...

SELECT training.SLNO,training.CATEGORY, SubString_Index(training.TRAINING_NAME, ',', 1) AS TRAINING_NAME FROM training UNION ALL SELECT training.SLNO,training.CATEGORY, SubString_Index(training.TRAINING_NAME, ',', -1) FROM training

我想获得下面给出的表格,请帮帮我

SLNO Category Training_name
1      A        1
1      A        5
1      A        9
1      A        15
1      A        12
1      A        16
2      B        2
2      B        6
2      B        10
2      B        17
3      C        1
3      C        3
3      C        7
3      C        19
3      C        14
3      C        18

3 个答案:

答案 0 :(得分:3)

DROP TABLE IF EXISTS my_bad_table;
DROP TABLE IF EXISTS my_good_table;

CREATE TABLE my_bad_table
(SLNO INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,Category CHAR(1) NOT NULL
,Training_name VARCHAR(200) NOT NULL
);

INSERT INTO my_bad_table VALUES
(1,'A','1,5,9,15,12,16'),
(2,'B','2,6,10,17'),
(3,'C','1,3,7,19,14,18');

CREATE TABLE my_good_table AS
SELECT DISTINCT x.SLNO
              , x.Category
              , CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(x.training_name,',',y.i+1),',',-1) AS UNSIGNED) training_name
           FROM my_bad_table x
              , (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 
                 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 8 UNION SELECT 9) y
          ORDER 
             BY slno
              , category
              , training_name;

SELECT * FROM my_good_table;
+------+----------+---------------+
| SLNO | Category | training_name |
+------+----------+---------------+
|    1 | A        |             1 |
|    1 | A        |             5 |
|    1 | A        |             9 |
|    1 | A        |            12 |
|    1 | A        |            15 |
|    1 | A        |            16 |
|    2 | B        |             2 |
|    2 | B        |             6 |
|    2 | B        |            10 |
|    2 | B        |            17 |
|    3 | C        |             1 |
|    3 | C        |             3 |
|    3 | C        |             7 |
|    3 | C        |            14 |
|    3 | C        |            18 |
|    3 | C        |            19 |
+------+----------+---------------+

如果1总是' A'等,那么需要进一步迈向规范化以消除冗余。

答案 1 :(得分:1)

这是一种方法:

select slno, category, substring_index(training_name, ',', 1) + 0 as training_id
from t
union all
select slno, category, substring_index(substring_index(training_name, ',', 2), ',', -1) + 0 as training_id
from t
where training_name like '%,%'
union all
select slno, category, substring_index(substring_index(training_name, ',', 3), ',', -1) + 0 as training_id
from t
where training_name like concat('%', repeat(',%', 2))
union all
select slno, category, substring_index(substring_index(training_name, ',', 4), ',', -1) + 0 as training_id
from t
where training_name like concat('%', repeat(',%', 3))
union all
 . . .

根据需要重复。将结果存储在新表中。修复外键引用和数据的其他方面。删除原始表(好吧,存档它),再也不要使用该结构。

答案 2 :(得分:0)

对我有用..

SELECT DISTINCT x.PARENT_SLNO, x.TRAINING_CATEGORY, CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(x.TRAINING_NAME,',',y.i+1),',',-1) AS UNSIGNED) TRAINING_NAME FROM assessment_training x, (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 8 UNION SELECT 9) y where PARENT_SLNO = 3 and TRAINING_CATEGORY='technical' ORDER BY PARENT_SLNO,TRAINING_CATEGORY,TRAINING_NAME