mysql模糊查詢的幾種方式

2021-10-07 07:33:25 字數 2712 閱讀 9879

-- 沒有用到索引

select

*from t_user where user_name like

'%test%'

;

-- 用到索引但是不通用

select

*from t_user where user_name like

'test%'

;

-- locate('substr',str,pos)

-- pos不填:返回substr在str中第一次出現的位置,如果不存在,返回值為0

-- pos存在:返回pos之後第一次出現的位置

select

*from t_user where locate(

'test'

,user_name)

>

0;

-- 同locate

select

*from t_user where position(

'test'

in user_name)

;

-- 查詢乙個字串在另乙個字串中首次出現的位置

select

*from t_user where instr(user_name,

'test'

)>

0;

find_in_set(str,strlist)

-- 測試及結果

mysql>

select find_in_set(

'1',

'1,2,3,4,5,6');

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

| find_in_set(

'1',

'1,2,3,4,5,6')|

+--------------------------------+|1

|+--------------------------------+

1row

inset

mysql>

select find_in_set(

'2',

'1,2,3,4,5,6');

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

| find_in_set(

'2',

'1,2,3,4,5,6')|

+--------------------------------+|2

|+--------------------------------+

1row

inset

mysql>

select find_in_set('',

'1,2,3,4,5,6');

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

| find_in_set('',

'1,2,3,4,5,6')|

+-------------------------------+|0

|+-------------------------------+

1row

inset

mysql>

select find_in_set(

'2','')

;+---------------------+

| find_in_set(

'2','')

|+---------------------+|0

|+---------------------+

1row

inset

mysql>

select find_in_set(

'7',

'1,2,3,4,5,6');

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

| find_in_set(

'7',

'1,2,3,4,5,6')|

+--------------------------------+|0

|+--------------------------------+

1row

inset

mysql>

select find_in_set(

null

,'1,2,3,4,5,6');

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

| find_in_set(

null

,'1,2,3,4,5,6')|

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

|null|+

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

1row

inset

mysql>

select find_in_set(

'7',

null);

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

| find_in_set(

'7',

null)|

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

|null|+

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

1row

inset

mysql模糊查詢的幾種方法

下面介紹mysql中模糊查詢的四種用法 1,表示任意0個或多個字元。可匹配任意型別和長度的字元,有些情況下若是中文,請使用兩個百分號 表示。比如 select from user where u name like 三 將會把u name為 張三 張貓三 三腳貓 唐三藏 等等有 三 的記錄全找出來。...

mysql模糊查詢的幾種方法

下面介紹mysql中模糊查詢的四種用法 1,表示任意0個或多個字元。可匹配任意型別和長度的字元,有些情況下若是中文,請使用兩個百分號 表示。比如 select from user where u name like 三 將會把u name為 張三 張貓三 三腳貓 唐三藏 等等有 三 的記錄全找出來。...

mysql模糊查詢的幾種方法

下面介紹mysql中模糊查詢的四種用法 1 表示任意0個或多個字元。可匹配任意型別和長度的字元,有些情況下若是中文,請使用兩個百分號 表示。比如 select from user where u name like 三 將會把u name為 張三 張貓三 三腳貓 唐三藏 等等有 三 的記錄全找出來。...