SQL模糊查詢排序問題

2022-06-17 05:30:14 字數 1211 閱讀 9069

drop table if exists `t_user`;

create table `t_user` (

`id` varchar(20) not null,

`name` varchar(20) default null,

`password` varchar(20) default null,

primary key (`id`)

) engine=innodb default charset=utf8;

insert into t_user values(1,'汪峰','888888');

insert into t_user values(2,'王汪峰','888888');

insert into t_user values(3,'汪峰峰','888888');

insert into t_user values(4,'歐陽汪峰','888888');

insert into t_user values(5,'王汪峰峰','888888');

select * from t_user where name like '%汪峰%';

結果為: 

可以發現,「汪峰峰」並沒有排列在「王汪峰」前面

select * from t_user f where f.name like '%汪峰%' order by 

(case

when f.name = '汪峰' then 1

when f.name like '汪峰%' then 2

when f.name like '%汪峰' then 3

when f.name like '%汪峰%' then 4

else 0

end ) limit 0,50;

查詢結果為: 

等於說額外新增了乙個虛擬列,來輔助進行排序; 

首先匹配完全==的,然後再按照規則匹配下一條資料,就能得到我們想要的結果了。

sql 模糊查詢

一般模糊語句如下 select 字段 from 表 where 某欄位 like 條件 其中關於條件,sql提供了四種匹配模式 1,表示任意0個或多個字元。可匹配任意型別和長度的字元,有些情況下若是中文,請使用兩個百分號 表示。比如 select from user where u name lik...

SQL模糊查詢

sql提供了四種匹配模式 1.表示任意 0個或多個字元。如下語句 select from user where name like 三 將會把 name為 張三 三腳貓 唐三藏 等等有 三 的全找出來 2.表示任意單個字元。語句 select from user where name like 三 ...

SQL模糊查詢

sql提供了四種模糊匹配方式 1,表示任意0個或多個字元。可匹配任意型別和長度的字元,有些情況下若是中文,請運用兩個百分號 表示。比如 select from user where u name like 三 將會把u name為 張三 張貓三 三腳貓 唐三藏 等等有 三 的記錄全找出來。另外,如果...