MySQL CASE statement and REGEXP

二次信任 提交于 2019-12-22 05:09:14

问题


I want to use a CASE statement that uses REGEXP. Currently I am doing something like this:

SELECT NAME,
 CASE INFO
   WHEN 'not cool' THEN 'Not Cool'
   WHEN 'very cool' THEN 'Cool'
 ELSE INFO
 END AS INFO
FROM INFO_TABLE

Is there any way to use REGEXP in the initial statement to make the condition act as a REGEXP? In theory this is what I want, which doesn't work:

SELECT NAME,    
 CASE INFO REGEXP
   WHEN 'not cool' THEN 'Not Cool'
   WHEN 'very cool' THEN 'Cool'
 ELSE INFO
 END AS INFO
FROM INFO_TABLE

I want 'not cool' and 'very cool' to be regular expressions. Hope that is clear enough.


回答1:


try this

select name,
case
  when info regexp 'not cool' then 'Not Cool'
  when info regexp 'very cool' then 'Cool'  
else 
  info
end 
  as info
from INFO_TABLE;



回答2:


I don't know if there is regex, but there are wilcards that you can use with LIKE.

The wildcard % gives you any number of characters. The wildcard _ gives you any single character.

Example:

SELECT email FROM people WHERE email LIKE '%harvard.edu'


来源:https://stackoverflow.com/questions/4755349/mysql-case-statement-and-regexp

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