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期 ...
随机推荐
- Django 再次学习笔记整理
url 路由系统 urlpatterns = [ # path('admin/', admin.site.urls), path('index/', views.index), re_path('^e ...
- 牛客网 牛可乐发红包脱单ACM赛 C题 区区区间间间
[题解] 我想到了两种解法. 一种是扫描线+线段树+单调栈. 另一种方法是O(n)的,比较巧妙. 考虑每个数在哪些区间可以作为最小数.最长的区间就是它向左右走,直到有数字比它小,这个可以用单调栈维护. ...
- MySQL 慢查询优化
为什么查询速度会慢 1.慢是指一个查询的响应时间长.一个查询的过程: 客户端发送一条查询给服务器 服务器端先检查查询缓存,如果命中了缓存,则立可返回存储在缓存中的结果.否则进入下一个阶段 服务器端进行 ...
- 安装weblogic时,运行configure.cmd报错、闪退、无法创建域
直接运行configure.cmd时在jar包加载完成时,不提示创建域的过程,而是直接退出程序 命令行: cd /d F:\00uep_rfs\wls1212_dev\wls12120 切换至解压路径 ...
- CactiI表结构和数据被动获取
cacti我们也用了很久了,但是它的表结构一直都没有去关心过,得空抽了半个晚上的时间,把它的库表结构大概看了下,某些字段的含义跟大家分享下:cacti的数据都是存放在rrdtool中的,数据库存放的其 ...
- .net对象的生命周期
阅读了文章:.NET对象生命周期小结 文章分多个部分,第一部分:介绍了,创建对象时,内存的分配,对象真正被创建,以及经历各阶段垃圾回收的过程. 第二部分,介绍了Finalize与Dispsose方法.
- ms sqlserver数据库建索引
索引分类:从物理结构上可分为两种:聚集索引和非聚集索引 (此外还有空间索引.筛选索引.XML索引) 因为聚集索引是索引顺序与物理存储顺序一致,所以只能建一个. 聚集索引就是把数据按主键顺序存储: 因为 ...
- C# easyui json类
using System; using System.Data; using System.Text; namespace Common { public class JsonHelp { priva ...
- POJ 1509 循环同构的最小表示法
题目大意: 给定一个字符串,可以把一段尾部接到头部,这样找到一个最小的字符串 方案一: 利用循环同构中找最小表示的方法来解决 论文参考http://wenku.baidu.com/view/438ca ...
- Java实验--继承与多态
---恢复内容开始--- 题目如下: [实验任务一]:面积计算(设计型). 1. 实验要求: 实验报告中要求包括程序设计思想.程序流程图.源代码.运行结果截图.编译错误分析等内容. 2.实验内容: ( ...