order by 特殊排序技巧
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
order by 特殊排序技巧的更多相关文章
- mysql如何用order by 自定义排序
mysql如何用order by 自定义排序 id name roleId aaa bbb ccc ddd eee ,MySQL可以通过field()函数自定义排序,格式:field(value,st ...
- Mysql Order By 字符串排序,mysql 字符串order by
Mysql Order By 字符串排序,mysql 字符串order by ============================== ©Copyright 蕃薯耀 2017年9月30日 http ...
- 【Python】 sort、sorted高级排序技巧
文章转载自:脚本之家 这篇文章主要介绍了python sort.sorted高级排序技巧,本文讲解了基础排序.升序和降序.排序的稳定性和复杂排序.cmp函数排序法等内容,需要的朋友可以参考下 Pyth ...
- mysql order by 中文 排序
mysql order by 中文 排序 1. 在MySQL中,我们经常会对一个字段进行排序查询,但进行中文排序和查找的时候,对汉字的排序和查找结果往往都是错误的. 这种情况在MySQL的很多版本中都 ...
- 【转载】 python sort、sorted高级排序技巧
这篇文章主要介绍了python sort.sorted高级排序技巧,本文讲解了基础排序.升序和降序.排序的稳定性和复杂排序.cmp函数排序法等内容,需要的朋友可以参考下 Python list内置so ...
- mysql select 无order by 默认排序 出现乱序的问题
原文:mysql select 无order by 默认排序 出现乱序的问题 版权声明:感谢您的阅读,转载请联系博主QQ3410146603. https://blog.csdn.net/newMan ...
- Order by 默认排序方式
--ORDER BY 默认排序方式为升序ASC:SELECT * FROM [TABLE_NAME] ORDER BY [COLUMN_NAME] ESC;--升序DESC:SELECT * FROM ...
- Order Siblings by 排序
在层次查询中,如果想让"亲兄弟"按规矩进行升序排序就需要使用ORDER SIBLINGS BY 这个特定的排序语句,若要降序输出可以在其后添加DESC关键字. 通过这个实验给大家展 ...
- 「Python实用秘技07」pandas中鲜为人知的隐藏排序技巧
本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第7期 ...
随机推荐
- POJ 3310 Caterpillar(图的度的判定)
题意: 给定一幅图, 问符不符合一下两个条件: (1) 图中没有环 (2)图中存在一条链, 点要么在链上, 要么是链上点的邻居. 分析: 建图,记录度数, 去掉所有度为1的点, 然后看看剩下是否是有2 ...
- PS日记一
shift+alt 从中心开始画圆 PHOTOSHOP是处理位图的软件, 栅格化是将矢量图形如:(Illustrator,或者CoreIDRAW中绘画的图形), 包括文字,这些矢量图文转换(也叫栅格化 ...
- 大数据学习——hive显示命令
show databases; desc t_partition001; desc extended t_partition002; desc formatted t_partition002; !c ...
- Action中result的各种转发类型
Action中result的各种转发类型 1,dispatcher:默认值 ,内部定向 <result>/WEB-INF/page/employeeAdd.jsp</result&g ...
- FZU2206函数求解
Problem 2206 函数求解 Accept: 154 Submit: 456 Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...
- [luoguP2015] 二叉苹果树(DP)
传送门 貌似是个树形背包... 好像吧.. f[i][j]表示节点i选条边的最优解 #include <cstdio> #include <cstring> #include ...
- BZOJ2662[BeiJing wc2012]冻结【SPFA】
“我要成为魔法少女!” “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切,封印于卡片之中„„” 在这个愿望被实现以后的世界里,人们享受着魔法卡片(SpellCard ...
- 【树状数组+dp】HDU 5542 The Battle of Chibi
http://acm.hdu.edu.cn/showproblem.php?pid=5542 [题意] 给定长为n的序列,问有多少个长为m的严格上升子序列? [思路] dp[i][j]表示以a[i]结 ...
- 跨多种环境部署 Gearman -改善应用程序性能和降低服务器负载
您可能想要将工作扩散到一个大型机器群体中,或者想要在不同语言和环境之间共享功能,那么开放源码的 Gearman 服务可以让您轻松地将工作分布到网络中的其他机器.本文将介绍 Gearman 的一些典型应 ...
- loj6169 相似序列(可持久化线段树)
题目: https://loj.ac/problem/6169 分析: 如果是要求两段序列全等的话,有一个套路: 对于{a1,a2,a3} {a4,a5,a6} 设一个素数p,那么如果p^a1+p^a ...