how to split a column into multiple columns in mysql

瘦欲@ 提交于 2019-12-23 05:16:18

问题


I have column like this


+--------------------------+
| Marks                    |
+--------------------------+
|Maths-80,Phy-100,Che-99   |
|Maths-90,Phy-60           | 
|Phy-82,Che-65             |
|Che-90                    |
|Maths-33,Phy-89,Che-65 | |Maths-50,Phy-43,Che-59 | +--------------------------+
i just give 3 sample subjects but in realdata it maybe any number of subjects seperated by comma. for that i want to display the marks for each subject by split the marks column and display it in multiple columns like the below sample.
 +-------------------------------------+-------------+------------+
| Marks                  | Maths       | Phy         | Che        |
+-------------------------------------+-------------+-------------+                               
Maths-80,Phy-100,Che-99  | 80          |   100      |     99      |
Maths-90,Phy-60          | 90          |   60       |     0       |
Phy-82,Che-65            |  0          |   82       |     65      |
Che-90                   | 90          |   0        |     0       |
Maths-33,Phy-89,Che-65   | 33          |   89       |     65      |
Maths-50,Phy-43,Che-59   | 50          |   43       |     59      |
+-------------------------------------+-------------+-------------+  

回答1:


With the given example data you can do something as below -

mysql> create table test (marks varchar(100));
Query OK, 0 rows affected (0.12 sec)

mysql> insert into test values ('Maths-80,Phy-100,Che-99'),('Maths-90,Phy-60'),('Phy-82,Che-65'),('Che-90'),('Maths-33,Phy-89,Che-65'),('Maths-50,Phy-43,Che-59');
Query OK, 6 rows affected (0.04 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select * from test ;
+-------------------------+
| marks                   |
+-------------------------+
| Maths-80,Phy-100,Che-99 |
| Maths-90,Phy-60         |
| Phy-82,Che-65           |
| Che-90                  |
| Maths-33,Phy-89,Che-65  |
| Maths-50,Phy-43,Che-59  |
+-------------------------+
6 rows in set (0.00 sec)

Now using locate and substring_index we can extract the values as

select marks, 
case 
 when locate('Maths',marks) > 0 then substring_index(substring_index(marks,'Maths-',-1),',',1) else 0 end 
as Maths ,
case 
 when locate('Phy',marks) > 0 then substring_index(substring_index(marks,'Phy-',-1),',',1) else 0 end 
as Phy ,
case 
 when locate('Che',marks) > 0 then substring_index(substring_index(marks,'Che-',-1),',',1) else 0 end 
as Che 
from test ;

Output :

+-------------------------+-------+------+------+
| marks                   | Maths | Phy  | Che  |
+-------------------------+-------+------+------+
| Maths-80,Phy-100,Che-99 | 80    | 100  | 99   |
| Maths-90,Phy-60         | 90    | 60   | 0    |
| Phy-82,Che-65           | 0     | 82   | 65   |
| Che-90                  | 0     | 0    | 90   |
| Maths-33,Phy-89,Che-65  | 33    | 89   | 65   |
| Maths-50,Phy-43,Che-59  | 50    | 43   | 59   |
+-------------------------+-------+------+------+
6 rows in set (0.00 sec)


来源:https://stackoverflow.com/questions/29093523/how-to-split-a-column-into-multiple-columns-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!