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

2022-09-15 16:27:14 字數 1677 閱讀 2773

mysql查詢如何判斷字段是否包含某個字串

有這樣乙個需求,在mysql資料庫字串字段(許可權)中,使用者有多個不同的郵箱,分別被『,』分開,現在要取出某個郵箱的所有成員列表。

假設有個表:?1

createtableusers(idint(6)notnullauto_increment,primarykey(id),user_namevarchar(20)notnull,emailsvarchar(50)notnull);

初始化表,並新增些記錄。?1

23truncatetableusers

insertintousers(user_name, emails)values('小張','[email protected],[email protected],[email protected]');

insertintousers(user_name, emails)values('小王','[email protected],[email protected],[email protected]');

mysql 中有些欄位是字串型別的,如何查詢其中包含某些字元的記錄呢?

方法一很簡單可以用%來實現比如?

1select*fromuserswhereemailslike"%[email protected]%";

但是這樣子的話就會把[email protected]ҳ查出來了就不符合需求了

方法二利用mysql 字串函式 find_in_set();?1

select*fromuserswherefind_in_set('[email protected]', emails);

這樣子就可以找到email欄位裡包含有[email protected]的所有使用者了

這樣就能達到我們預期的效果,問題就解決了!

要注意的是:mysql字串函式 find_in_set(str1,str2)函式是返回str2中str1所在的位置索引,str2必須以","分割開。

Mysql查詢 如何判斷某欄位是否包含某個字串?

今天乙個群有人向我求助這樣乙個問題,它的mysql表結構和表資料如下圖 mysql查詢 如何判斷某欄位是否包含某個字串 現在,這位群友的要求是 title欄位有1和3的都要查出來,應該如何做?1 單個值查詢使用myql函式 find in set select fromby contentwhere...

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

mysql查詢如何判斷字段是否包含某個字串 有這樣乙個需求,在mysql 資料庫字串字段 許可權 中,使用者有多個不同的郵箱,分別被 分開,現在要取出某個郵箱的所有成員列表。假設有個表 create table users id int 6 not null auto increment,prima...

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

有這樣乙個需求,在mysql資料庫字串字段 許可權 中,使用者有多個不同的郵箱,分別被 分開,現在要取出某個郵箱的所有成員列表。假設有個表 create table users id int 6 not null auto increment,primary key id user name var...