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()
  1. if object_id('tempdb..#temp') is not null
  2. drop table #temp
  3. create table #temp(col1 varchar(100), col2 varchar(100))
  4. insert into #temp
  5. select 'X68.23','4'
  6. union all select 'X86.32','2'
  7. union all select 'ZA11.30','1'
  8. union all select 'ZB11.47','1'
  9. go
  10. ------ 需求一、按col 4,1,2排序
  11. -- 用 charindex
  12. select col1,col2 from #temp
  13. order by charindex(col2,'4,1,2')
  14. -- order by 中用 case
  15. select col1,col2 from #temp
  16. order by
  17. case col2
  18. when '4' then 1
  19. when '1' then 2
  20. when '2' then 3
  21. else 3 + rand()
  22. end
  23. -- 用 union
  24. select col1,col2 from #temp where col2 = '4' union all
  25. select col1,col2 from #temp where col2 = '1' union all
  26. select col1,col2 from #temp where col2 = '2'
  27. ------ 需求二、 col 4 排第一,其余随便
  28. select col1,col2 from #temp
  29. order by
  30. case col1
  31. when '4' then 1
  32. else 1 + rand()
  33. end
  34. ------ 需求三、 随机排序
  35. select col1,col2 from #temp
  36. order by newid()
  37. ORDER BY
    case when state=5 then 2 else 1 end ASC,
    weight_op_time DESC

order by 特殊排序技巧的更多相关文章

  1. mysql如何用order by 自定义排序

    mysql如何用order by 自定义排序 id name roleId aaa bbb ccc ddd eee ,MySQL可以通过field()函数自定义排序,格式:field(value,st ...

  2. Mysql Order By 字符串排序,mysql 字符串order by

    Mysql Order By 字符串排序,mysql 字符串order by ============================== ©Copyright 蕃薯耀 2017年9月30日 http ...

  3. 【Python】 sort、sorted高级排序技巧

    文章转载自:脚本之家 这篇文章主要介绍了python sort.sorted高级排序技巧,本文讲解了基础排序.升序和降序.排序的稳定性和复杂排序.cmp函数排序法等内容,需要的朋友可以参考下 Pyth ...

  4. mysql order by 中文 排序

    mysql order by 中文 排序 1. 在MySQL中,我们经常会对一个字段进行排序查询,但进行中文排序和查找的时候,对汉字的排序和查找结果往往都是错误的. 这种情况在MySQL的很多版本中都 ...

  5. 【转载】 python sort、sorted高级排序技巧

    这篇文章主要介绍了python sort.sorted高级排序技巧,本文讲解了基础排序.升序和降序.排序的稳定性和复杂排序.cmp函数排序法等内容,需要的朋友可以参考下 Python list内置so ...

  6. mysql select 无order by 默认排序 出现乱序的问题

    原文:mysql select 无order by 默认排序 出现乱序的问题 版权声明:感谢您的阅读,转载请联系博主QQ3410146603. https://blog.csdn.net/newMan ...

  7. Order by 默认排序方式

    --ORDER BY 默认排序方式为升序ASC:SELECT * FROM [TABLE_NAME] ORDER BY [COLUMN_NAME] ESC;--升序DESC:SELECT * FROM ...

  8. Order Siblings by 排序

    在层次查询中,如果想让"亲兄弟"按规矩进行升序排序就需要使用ORDER SIBLINGS BY 这个特定的排序语句,若要降序输出可以在其后添加DESC关键字. 通过这个实验给大家展 ...

  9. 「Python实用秘技07」pandas中鲜为人知的隐藏排序技巧

    本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第7期 ...

随机推荐

  1. SpringMVC中的几种事务管理器

    转载https://blog.csdn.net/qq_26222859/article/details/52032853 1JDBC及iBATIS.MyBatis框架事务管理器 <bean id ...

  2. [WPF自定义控件]使用WindowChrome自定义Window Style

    1. 为什么要自定义Window 对稍微有点规模的桌面软件来说自定义的Window几乎是标配了,一来设计师总是克制不住自己想想软件更个性化,为了UI的和谐修改Window也是必要的:二来多一行的空间可 ...

  3. 新建oracle连接远程服务

    更新下面两个文件夹中的 D:\app\shisan\product\11.2.0\client_1\network\admin D:\ORACLE\product\11.2.0\dbhome_1\NE ...

  4. FTS5与DIY

    此文已由作者王荣涛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. FTS5简介 前文已经介绍了FTS3/FTS4,本文着重介绍它们的继任者FTS5. FTS5是在SQLite ...

  5. CI 安装时目录的安全处理

    如果你想通过隐藏 CodeIgniter 的文件位置来增加安全性,你可以将 system 和 application 目录修改为其他的名字,然后打开主目录下的 index.php 文件将 $syste ...

  6. 如何取SQL结果集的第一条记录

    在SQL Server数据库中,使用top关键字: SELECT TOP number|percent column_name(s) FROM table_name 在MySQL数据库中,使用LIMI ...

  7. ES6__变量的解构赋值

    /* 变量的解构赋值 */ /* 基本概念 : 本质上就是一种匹配模式,只要等号两边的模式相同,那么左边的变量就可以被赋予对应的值. 结构赋值主要分为: 1. 数组的解构赋值 2. 对象的结构赋值 3 ...

  8. React Native资料汇总

    React Native 官方文档中文版翻译 http://wiki.jikexueyuan.com/project/react-native/homepage.html REACT NATIVE开发 ...

  9. GOF 23种设计模式目录

    经典的gof 23种设计模式,目录大纲查看. 1. Singleton(单例模式) 保证一个类只有一个实例,并提供访问它的全局访问点. 2. Abstract Factory(抽象工厂模式) 提供一个 ...

  10. 关于用String Calender类 计算闰年的Demo

    package cn.zmh.zuoye; import java.util.Calendar; public class StringRun { public static void main(St ...