order by 特殊排序技巧
2024-09-27 11:39:48
if object_id('tempdb..#temp') is not null
drop table #temp create table #temp(col1 varchar(100), col2 varchar(100))
insert into #temp
select 'X68.23',''
union all select 'X86.32',''
union all select 'ZA11.30',''
union all select 'ZB11.47',''
go ------ 需求一、按col 4,1,2排序
-- 用 charindex
select col1,col2 from #temp
order by charindex(col2,'4,1,2') -- order by 中用 case
select col1,col2 from #temp
order by
case col2
when '' then 1
when '' then 2
when '' then 3
else 3 + rand()
end -- 用 union
select col1,col2 from #temp where col2 = '' union all
select col1,col2 from #temp where col2 = '' union all
select col1,col2 from #temp where col2 = '' ------ 需求二、 col 4 排第一,其余随便
select col1,col2 from #temp
order by
case col1
when '' then 1
else 1 + rand()
end ------ 需求三、 随机排序
select col1,col2 from #temp
order by newid()
- if object_id('tempdb..#temp') is not null
- drop table #temp
- create table #temp(col1 varchar(100), col2 varchar(100))
- insert into #temp
- select 'X68.23','4'
- union all select 'X86.32','2'
- union all select 'ZA11.30','1'
- union all select 'ZB11.47','1'
- go
- ------ 需求一、按col 4,1,2排序
- -- 用 charindex
- select col1,col2 from #temp
- order by charindex(col2,'4,1,2')
- -- order by 中用 case
- select col1,col2 from #temp
- order by
- case col2
- when '4' then 1
- when '1' then 2
- when '2' then 3
- else 3 + rand()
- end
- -- 用 union
- select col1,col2 from #temp where col2 = '4' union all
- select col1,col2 from #temp where col2 = '1' union all
- select col1,col2 from #temp where col2 = '2'
- ------ 需求二、 col 4 排第一,其余随便
- select col1,col2 from #temp
- order by
- case col1
- when '4' then 1
- else 1 + rand()
- end
- ------ 需求三、 随机排序
- select col1,col2 from #temp
- order by newid()
ORDER BY
case when state=5 then 2 else 1 end ASC,
weight_op_time DESC
最新文章
- 怎样两个月完成Udacity Data Analyst Nanodegree
- 第一个Java程序HelloWorld
- .net学习之继承、里氏替换原则LSP、虚方法、多态、抽象类、Equals方法、接口、装箱拆箱、字符串
- MYSQL存储过程实现in传入参数 where in('1','2')
- 【sgu282】Isomorphism
- iOS 视频播放的简单使用
- Xamarin.forms 自定义dropdownview控件
- spark2.1操作json(save/read)
- 利用Django进行Web开发
- T4代码生成脚本从添加注释,添加命名空间开始(一款强大的代码生成工具从看懂脚本,到随心所欲的玩弄代码,本文只是T4的冰山一角,博主将不断深入探索并完善该文章)
- 局域网 FTP建立,搭建一个简易的局域网服务器
- 用UDP实现聊天功能
- [httpd] httpd directory list character encoding
- MYSQL 中的 int(11) 到底代表什么意思?
- python selenium操作表格式元素实例
- Build-in Function:abs(),all(),any(),assii(),bin(),issubclass(),bytearray(),isinstance()
- WCF入门学习1-最简单的一次通信
- 全新办公方式,iClap引领企业级服务新浪潮
- 2017ACM暑期多校联合训练 - Team 1 1002 HDU 6034 Balala Power! (字符串处理)
- 将EXCEL表中的数据轻松导入Mysql数据表
热门文章
- 【BZOJ 2761】 不重复数字 (哈希算法)
- STL中set求交集、并集、差集的方法
- 在不是modelAttribute的情况下,如何保存页面输入值的方法(多行遍历)
- luogu4035 [JSOI2008]球形空间产生器
- FZU2102Solve equation
- 在oracle下我们如何正确的执行数据库恢复
- POJ 2375 Cow Ski Area【tarjan】
- bzoj1190 [HNOI2007]梦幻岛宝珠 动态规划
- Borg Maze-POJ3026(bfs+最小生成树)
- 洛谷——P2256 一中校运会之百米跑