order by能不能自己指定排序方法

bl_xin 2004-11-22 01:44:31
order by除了顺序逆序,能不能我自己指定方式,比方说,某列全部由整数1-4组成,我要按3-2-1-4的顺序排序,能做到么?
...全文
549 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsxaa 2004-11-22
  • 打赏
  • 举报
回复
这样可以了 厉害
bl_xin 2004-11-22
  • 打赏
  • 举报
回复
牛!
txlicenhe 2004-11-22
  • 打赏
  • 举报
回复

order by charindex(','+cast(b as varchar())+',',',3,2,1,4,')
txlicenhe 2004-11-22
  • 打赏
  • 举报
回复
order by charindex(cast(b as char(1)),'3214')

这样如果有 21 怎么办
-----------------------------
order by charindex(','+cast(b as char(1))+',',',3,2,1,4,')


这样总可以了吧。
lsxaa 2004-11-22
  • 打赏
  • 举报
回复
order by charindex(cast(b as char(1)),'3214')

这样如果有 21 怎么办
lsxaa 2004-11-22
  • 打赏
  • 举报
回复
order by case when col=4 then 1 else 0 end,col desc
txlicenhe 2004-11-22
  • 打赏
  • 举报
回复
测试:
create table test(a int identity(1,1), b int)

insert into test (b)select 1
insert into test (b)select 3
insert into test (b)select 2
insert into test (b)select 4
insert into test (b)select 1

select * from test
order by charindex(cast(b as char(1)),'3214')

/*结果
a b
----------- -----------
2 3
3 2
1 1
5 1
4 4

(所影响的行数为 5 行)
*/

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧