SQL 語句中的in、find_in_set、like的區別

1.in查詢相當于多個or條件的疊加

例如:

select * from user where user_id in (1,2,3);

等效于

select * from user where user_id = 1 or user_id = 2 or user_id = 3;

not in與in相反,如下

select * from user where user_id not in (1,2,3);

等效于

select * from user where user_id != 1 and user_id != 2 and user_id != 3;

2.find_in_set

find_in_set基本語法

FIND_IN_SET(str,strlist)

str 要查詢的字符串,strlist 字段名 參數以”,”分隔 如 (1,2,6,8)

如果str不在strlist 或strlist 為空字符串,則返回值為 0 。如任意一個參數為NULL,則返回值為 NULL。這個函數在第一個參數包含一個逗號(‘,’)時將無法正常運行。

+----+---------+-----------+-------------+

| id | user_id | follow_id | follow_time |

+----+---------+-----------+-------------+

| 13 | 15 ? ? ?| 16,15 ? ? | ?1478096138 |

| 14 | 15 ? ? ?| 17 ? ? ? ?| ?1478177725 |

| 15 | 15 ? ? ?| 19 ? ? ? ?| ?1478181035 |

+----+---------+-----------+-------------+

比如這張表,SELECT * from test where FIND_IN_SET('5',follow_id);這樣是查不到的,返回值為null,因為follow_id中沒有”5”這個值,它不同于 like 模糊查詢,它是以“,”來分隔值

3.like

like是廣泛的模糊匹配,字符串中沒有分隔符,Find_IN_SET 是精確匹配,字段值以英文”,

”分

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容