码迷,mamicode.com
首页 > 数据库 > 详细

Mysql字符串字段判断是否包含某个字符串的3种方法

时间:2016-12-21 01:57:25      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:完美解决   str   sel   nbsp   .com   title   理解   例子   mail   

方法一:

SELECT * FROM users WHERE emails like "%b@email.com%";

方法二:

利用MySQL 字符串函数 find_in_set();

SELECT * FROM users WHERE find_in_set(‘aa@email.com‘, emails);

这样是可以的,怎么理解呢?

mysql有很多字符串函数 find_in_set(str1,str2)函数是返回str2中str1所在的位置索引,str2必须以","分割开。

注:当str2为NO1:“3,6,13,24,33,36”,NO2:“13,33,36,39”时,判断两个数据中str2字段是否包含‘3’,该函数可完美解决

mysql > SELECT find_in_set()(‘3‘,‘3,6,13,24,33,36‘) as test;
-> 1

mysql > SELECT find_in_set()(‘3‘,‘13,33,36,39‘) as test;
-> 0

方法三:

使用locate(substr,str)函数,如果包含,返回>0的数,否则返回0 

例子:判断site表中的url是否包含‘http://‘子串,如果不包含则拼接在url字符串开头
update site set url =concat(‘http://‘,url) where locate(‘http://‘,url)=0 

注意mysql中字符串的拼接不能使用加号+,用concat函数

Mysql字符串字段判断是否包含某个字符串的3种方法

标签:完美解决   str   sel   nbsp   .com   title   理解   例子   mail   

原文地址:http://www.cnblogs.com/lcngu/p/6200491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!