mysql判断字符串包含的方式

方法一:

SELECT * FROM users WHERE emails like "%[email protected]%";

方法二:

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

SELECT * FROM users WHERE find_in_set('[email protected]', 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函数

DIY :

instr(Set,Str)

你可能感兴趣的:(mysql判断字符串包含的方式)