日期:2014-05-18 浏览次数:20592 次
create table t1 (title varchar(10),tags varchar(20))
insert t1
select 'abc','zxc,vbn,qwer' union all
select 'vbn','tyu,uio,vbn' union all
select 'vbn','tyu,uio,vbn' union all
select 'abc','abc,vbn,tyu' union all
select 'vbn','tyu,uio' union all
select 'vbn',null union all
select null,'vbn'
go
select * 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    vbn
abc    zxc,vbn,qwer
vbn    tyu,uio,vbn
vbn    tyu,uio,vbn
abc    abc,vbn,tyu
vbn    tyu,uio
vbn    NULL
*/
go
drop table t1