首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

like 模糊查询语句解决方案

2012-02-02 
like 模糊查询语句表A有 中字段title(标题) ,tags(关键字,多个关键字用,逗号隔开)现在进行关键字查询 先进

like 模糊查询语句
表A有 中字段 title(标题) ,tags(关键字,多个关键字用,逗号隔开)

现在进行关键字查询 先进行所有的 tags 匹配,如果没有再进行 title匹配,要求 有tags关键字匹配 排在前面,然后再排title匹配的

[解决办法]
order by case when charindex(','+titile+',',','+关键字+',')>0 then 1 else 0 end
[解决办法]
以查找"vbn"为例

SQL code
create table t1 (title varchar(10),tags varchar(20))insert t1select 'abc','zxc,vbn,qwer' union allselect 'vbn','tyu,uio,vbn' union allselect 'vbn','tyu,uio,vbn' union allselect 'abc','abc,vbn,tyu' union allselect 'vbn','tyu,uio' union allselect 'vbn',null union allselect null,'vbn'goselect * from t1-- where-- tags like '%vbn%'-- or-- title like '%vbn%'order by (case when charindex(','+tags+',',',vbn,')>0 then 0       when tags is null then 2      else 1      end)/*(所影响的行数为 7 行)title    tags-----  -------NULL    vbnabc    zxc,vbn,qwervbn    tyu,uio,vbnvbn    tyu,uio,vbnabc    abc,vbn,tyuvbn    tyu,uiovbn    NULL*/godrop table t1 

热点排行