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

最新文章

  1. 怎样两个月完成Udacity Data Analyst Nanodegree
  2. 第一个Java程序HelloWorld
  3. .net学习之继承、里氏替换原则LSP、虚方法、多态、抽象类、Equals方法、接口、装箱拆箱、字符串
  4. MYSQL存储过程实现in传入参数 where in('1','2')
  5. 【sgu282】Isomorphism
  6. iOS 视频播放的简单使用
  7. Xamarin.forms 自定义dropdownview控件
  8. spark2.1操作json(save/read)
  9. 利用Django进行Web开发
  10. T4代码生成脚本从添加注释,添加命名空间开始(一款强大的代码生成工具从看懂脚本,到随心所欲的玩弄代码,本文只是T4的冰山一角,博主将不断深入探索并完善该文章)
  11. 局域网 FTP建立,搭建一个简易的局域网服务器
  12. 用UDP实现聊天功能
  13. [httpd] httpd directory list character encoding
  14. MYSQL 中的 int(11) 到底代表什么意思?
  15. python selenium操作表格式元素实例
  16. Build-in Function:abs(),all(),any(),assii(),bin(),issubclass(),bytearray(),isinstance()
  17. WCF入门学习1-最简单的一次通信
  18. 全新办公方式,iClap引领企业级服务新浪潮
  19. 2017ACM暑期多校联合训练 - Team 1 1002 HDU 6034 Balala Power! (字符串处理)
  20. 将EXCEL表中的数据轻松导入Mysql数据表

热门文章

  1. 【BZOJ 2761】 不重复数字 (哈希算法)
  2. STL中set求交集、并集、差集的方法
  3. 在不是modelAttribute的情况下,如何保存页面输入值的方法(多行遍历)
  4. luogu4035 [JSOI2008]球形空间产生器
  5. FZU2102Solve equation
  6. 在oracle下我们如何正确的执行数据库恢复
  7. POJ 2375 Cow Ski Area【tarjan】
  8. bzoj1190 [HNOI2007]梦幻岛宝珠 动态规划
  9. Borg Maze-POJ3026(bfs+最小生成树)
  10. 洛谷——P2256 一中校运会之百米跑