SQL ORDER by `no` with NULLs at the end

六眼飞鱼酱① 提交于 2019-11-30 17:42:05

Could you try this?

ORDER BY ISNULL(no),no;

You can use a CASE statement to tweak ordering:

SELECT * 
FROM table 
ORDER BY case when no is null then 2 else 1 end, no

This orders on "nullableness" first, and no second.

Thompson
SELECT * FROM table ORDER BY ISNULL(field), field ASC;
SELECT * FROM table ORDER BY COALESCE(no,999999) ASC

Just replace the 999999 with something larger if your numbers are naturally bigger than that.

Ok, I think I got it:

SELECT * FROM table WHERE no IS NOT NULL ORDER BY no ASC UNION
SELECT * FROM table WHERE no IS NULL

Or is there any better way ?

SELECT * FROM table ORDER BY no ASC NULLS LAST

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!