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函數。
轉載:https://blog.csdn.net/hechurui/article/details/49278493

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章