加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sqlserver使用order by case when进行优先级排序

发布时间:2020-12-12 14:15:14 所属栏目:MsSql教程 来源:网络整理
导读:? -- 创建表 create table tNews (NewsId int identity(1,1) primary key, Keyword nvarchar(50) not null, Title nvarchar(100) null, Contents nvarchar(1000) null, Remark nvarchar(1000)) ? ? -- 向表中插入数据 insert into tNews(Keyword,Title,Conte

?

--创建表

create table tNews

(NewsId int identity(1,1) primary key,

Keyword nvarchar(50) not null,

Title nvarchar(100) null,

Contents nvarchar(1000) null,

Remark nvarchar(1000))

?

?

--向表中插入数据

insert into tNews(Keyword,Title,Contents,Remark)values ('csd','x','a')

insert intotNews(Keyword,Remark) values ('asd','ax',Remark) values ('cad','xa',Remark) values ('csadx','xy')

insert intotNews(Keyword,Remark) values ('csdn','sdax',Remark) values ('csdns',Remark) values ('a113ns','s3x','3ax',Remark) values ('没有','szx','mx','mei')

?

--查询表数据

select*from tNews

--关键字含有a的放在最前面的行,标题含有a的放在次于关键字含有a的行

没有含有a的放在最后

依次排序为:Keyword>Title> Contents>Remark

★排序Sql语句:

select NewsId,Keyword,Remarkfrom tNews

order by

case when Keyword like '%a%' then 1 else 10end,

case when Title like '%a%' then 2 else 10end,

case when Contents like '%a%' then 3 else10 end,

case when Remark like '%a%' then 4 else 10end

★排序后的结果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读