如何在MySQL中将一列拆分为多列 [英] how to split a column into multiple columns in mysql

查看:2399
本文介绍了如何在MySQL中将一列拆分为多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的专栏


+--------------------------+
| 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 | +--------------------------+

我只给出了3个示例主题,但在realdata中,它可能是由逗号分隔的任意数量的主题.为此,我想通过拆分标记列来显示每个主题的标记,并像下面的示例一样在多列中显示它.

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      |
+-------------------------------------+-------------+-------------+  

推荐答案

使用给定的示例数据,您可以执行以下操作-

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)

现在使用locatesubstring_index,我们可以将值提取为

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 ;

输出:

+-------------------------+-------+------+------+
| 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)

这篇关于如何在MySQL中将一列拆分为多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆