MySql从匹配字符串点开始选择

时间:2018-05-10 10:21:40

标签: mysql

我有一张表

col1     col2
---------------
aaaa  |  id=456,some_other_strings
bbbb  |  examno=3322,any_other_strings
cccc  |  id=587,some_other_strings

我想从col2中匹配任一字符串(id = and,some_other_strings)或(examno = and,some_other_strings)的点开始和结束选择,以便最终输出如下所示

col1     col2
---------------
aaaa  |  456
bbbb  |  3322
cccc  |  587

这可能吗?

2 个答案:

答案 0 :(得分:1)

使用案例和substring_index

select 
  case when col2 like 'id=%'
  then substring_index(substring_index(col2, 'id=',-1),',',1)
  else substring_index(substring_index(col2, 'examno=',-1),',',1)
  end col2
from demo

Demo

答案 1 :(得分:0)

SET @x = 'id=456,some_other_string';

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(@x,'=',-1),',',1) x;
+------+
| x    |
+------+
| 456  |
+------+