方法一:like
SELECT * FROM 表名 WHERE 字段名 like"%字符%";
方法二:find_in_set()
利用mysql 字符串函數(shù) find_in_set();
SELECT * FROM users WHERE find_in_set('字符', 字段名);
這樣是可以的,怎么理解呢?
mysql有很多字符串函數(shù) find_in_set(str1,str2)函數(shù)是返回str2中str1所在的位置索引,str2必須以","分割開(kāi)。
注:當(dāng)str2為NO1:“3,6,13,24,33,36”,NO2:“13,33,36,39”時(shí),判斷兩個(gè)數(shù)據(jù)中str2字段是否包含‘3’,該函數(shù)可完美解決
mysql > SELECT find_in_set()('3','3,6,13,24,33,36') as test;-> 1mysql > SELECT find_in_set()('3','13,33,36,39') as test;-> 0
方法三:locate(字符,字段名)
使用locate(字符,字段名)函數(shù),如果包含,返回>0的數(shù),否則返回0 ,
它的別名是 position in
select*from表名where locate(字符,字段)select*from表名whereposition(字符in 字段);
例子:判斷site表中的url是否包含'http://'子串,如果不包含則拼接在url字符串開(kāi)頭
update site set url =concat('http://',url) where locate('http://',url)=0?
注意mysql中字符串的拼接不能使用加號(hào)+,用concat函數(shù)
?方法四:INSTR(字段,字符)
select*from表名whereINSTR(字段,字符)
網(wǎng)上說(shuō)模糊查詢(xún)?用?locate?速度快,不知道結(jié)論怎么來(lái)的,可能是大數(shù)據(jù)量的情況下吧。