order by 的时候,如何把null值排在最后面??

chuanyi 2006-01-24 10:27:20
表A:字段:ID
1,2,null,3,null,4,5…………

我想排序成:
1,2,3,4,5,null,null…………

最好不要使用union,因为union 不能使用两个order by,如果把order by 放在最后面,那就成了
null,null,null,1,2,3,4,5……
也不能使用select * from (select * from )这种复合的语句
...全文
1224 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
terence4444 2006-01-24
  • 打赏
  • 举报
回复
select *
from A
ORDER BY (case WHEN id is NULL then 1 else 0 end)
lsqkeke 2006-01-24
  • 打赏
  • 举报
回复
简化一下:
select * from @u
order by (case when a is null then (select max(a) from @u)+1 else a end )
lsqkeke 2006-01-24
  • 打赏
  • 举报
回复
那就这样啊:
select * from @u order by (case when a is null then cast((select max(a) from @u where a is not null)as int)+1 else a end )
lsqkeke 2006-01-24
  • 打赏
  • 举报
回复
to 楼上 : 你的排序null在前面



-狙击手- 2006-01-24
  • 打赏
  • 举报
回复
select * from @t order by (case when id is null then (select max(id)+1 from @t) else id end )
-狙击手- 2006-01-24
  • 打赏
  • 举报
回复
select * from @t order by (case when id is null then (select max(id)+1 from @t) else id end ) desc

-狙击手- 2006-01-24
  • 打赏
  • 举报
回复
错了,再想
-狙击手- 2006-01-24
  • 打赏
  • 举报
回复
select * from tb order by (case when id is null then 1 else - id end ) desc
chuanyi 2006-01-24
  • 打赏
  • 举报
回复
不行,如果id超過999999999呢?id是不定的
lsqkeke 2006-01-24
  • 打赏
  • 举报
回复
select * from tb order by (case when id is null then 999999 else id end )

999999那个数设置成比最大ID 还大的数字

34,837

社区成员

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

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