Mysql字串字段判斷是否包含某個字串的方法

2021-10-01 23:43:43 字數 1089 閱讀 4791

方法一:like

select * from 表名 where 欄位名 like "%字元%";
方法二:find_in_set()

利用mysql 字串函式 find_in_set();

select * from users where find_in_set('字元', 欄位名);
這樣是可以的,怎麼理解呢?

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(字元,欄位名)

使用locate(字元,欄位名)函式,如果包含,返回》0的數,否則返回0 ,

它的別名是 position in

select * from 表名 where locate(字元,字段)

select * from 表名 where position(字元 in 字段);

例子:判斷site表中的url是否包含'http://'子串,如果不包含則拼接在url字串開頭

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

方法四:instr(字段,字元)

select * from 表名 where instr(字段,字元)

Mysql 字串字段,如何判斷是否包含某個字串

假設有個表 create table users id int 6 not null auto increment,primary key id user name varchar 20 not null,emails varchar 50 not null 初始化表,並新增些記錄。truncate...

mysql 判斷某欄位是否包含某特定字串

今天遇到寫功能時遇到乙個問題,資料庫中的字串為list形式,多個字串間逗號隔開 假如此時需要查詢該列中包含的 ssdc1 欄位的資料,理論上可以用如下函式 select from xx where ssdc code like ssdc1 select from xx where locate ss...

字串 判斷是否

字串判斷的所有 isdigit 是否全是數字 isalpha 是否全是由字母組成 返回true isalnum 是否由字母和數字組成 islower 是否都是小寫字母 isupper 是否都是大寫字母 istitle 是否英文本母首字母都是大寫 isalpha 是否全是英文 中文的漢字會被判為tru...