从匹配字符或字符串的表中获取列表

时间:2016-04-05 13:51:41

标签: mysql

如何进行将username

返回结果的查询
  • o
  • 开头
  • 之间的某处包含o
  • o
  • 结尾

我在尝试:

select username from userinfo where username in('%o' , '%o%' , '%o')

但上述查询并未返回任何结果。这可能是什么原因?我怎么能得到结果?

5 个答案:

答案 0 :(得分:2)

您必须使用LIKE

select username 
from userinfo 
where username LIKE '%o' OR username LIKE '%o%' OR username LIKE '%o'

或者,如果您没有位数据集并且正在寻找更简洁的语法,则可以使用REPLACE

select username 
from userinfo 
where REPLACE(username, 'o', '') <> username

答案 1 :(得分:1)

如评论中所述,您需要LIKE运算符:

SELECT username
FROM userinfo
WHERE 
username LIKE '%o'  OR
username LIKE '%o%' OR
username LIKE '%o

答案 2 :(得分:1)

试试这个:

select username 
from userinfo  
where username like '%o'  
OR username like '%o%'  
OR username like '%o';

答案 3 :(得分:1)

您也可以尝试使用REGEXP

select username 
from userinfo 
WHERE username REGEXP 'o'

或喜欢

SELECT username FROM userinfo WHERE find_in_set('o', username)>0

答案 4 :(得分:1)

需要改进思考

包含o之间的某处,包括开始和结束。所以你只需要

select username from userinfo where username like '%o%'