mysql判斷乙個字串是否包含某子串

2021-08-16 20:13:16 字數 889 閱讀 8548

mysql字串字段判斷是否包含某個字串的3種方法

方法一:

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字串開頭

注意mysql中字串的拼接不能使用加號+,用concat函式

天下國家,可均也;爵祿,可辭也;白刃,可蹈也;中庸不可能也

SQL 判斷乙個字串是否在另外乙個字串中

eg str1 admin str2 1234,123admin,xcxx 比較str1是否在str2中 用常用的charindex,返回肯定是有值的,這裡自己動手寫乙個方法 檢查乙個字串是否在另外乙個字串中數,另外乙個字串元素用,隔開 create function dbo checkstrina...

判斷乙個字串是否在另乙個字串中

方法一 string str1 nihaoksdoksad string str2 ok int total 0 for string tmp str1 tmp null tmp.length str2.length tmp tmp.substring 1 system.out.println st...

判斷乙個字串是否為另外乙個字串旋轉之後的字串

例如 給定s1 aabcd和s2 bcdaa,返回1 給定s1 abcd和s2 acbd,返回0.aabcd左旋乙個字元得到abcda aabcd右旋乙個字元得到daabc 思路 把aabcd複製兩遍,看結果是否在aabcdaabcd 中 左旋和右旋的結果都在aabcdaabcd 中 include...