基本上我有一个这样的专栏:
+----------------+
| Marks |
+----------------+
|Maths,Phy,Che |
|Maths,Phy |
|Phy,Che |
|Che |
|Maths,Phy,Che |
|Maths,Phy,Che |
+----------------+
我想成为这样的
+-------------------------+-------+------+------+
| marks | Maths | Phy | Che |
+-------------------------+-------+------+------+
| Maths,Phy,Che | 1 | 1 | 1 |
| Maths,Phy | 1 | 1 | 0 |
| Phy,Che | 0 | 1 | 1 |
| Che | 0 | 0 | 1 |
| Maths,Phy,Che | 1 | 1 | 1 |
| Maths,Phy,Che | 1 | 1 | 1 |
+-------------------------+-------+------+------+
我试图使用此答案(how to split a column into multiple columns in mysql)
中公布的子字符串但我无法按照我想要的方式工作
答案 0 :(得分:0)
可以使用类似值的结果
select marks
, (marks like '%Maths%') as Maths
, (marks like '%Phy%') as Phy
, (marks like '%Che%') as Che
from my table
答案 1 :(得分:0)
一种选择是使用MySQL FIND_IN_SET
功能。如果返回0或NULL,则在集合中找不到该项。如果它返回非零,则表示该集合中项目的位置。我们可以测试函数的返回值,只返回零或一。
这样的事情:
SELECT t.marks
, IF(FIND_IN_SET( 'Maths' ,t.marks),1,0) AS `Maths`
, IF(FIND_IN_SET( 'Phy' ,t.marks),1,0) AS `Phy`
, IF(FIND_IN_SET( 'Che' ,t.marks),1,0) AS `Che`
FROM column_like_this t