如果 Column1 不为空,则按 Column1 排序,否则按 Column2 排序 [英] Order by Column1 if Column1 is not null, otherwise order by Column2

查看:59
本文介绍了如果 Column1 不为空,则按 Column1 排序,否则按 Column2 排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 sql 中组合 ORDER BYIS NULL 以便我可以在列不为空时按列排序,但如果是null,按另一列排序?

Is there a way to combine ORDER BY and IS NULL in sql so that I can order by a column if the column isn't null, but if it is null, order by another column?

推荐答案

类似:

ORDER BY CASE 
    WHEN Column1 IS NOT NULL THEN Column1
    ELSE Column2
END

与写作相同:

ORDER BY COALESCE(Column1, Column2)

两者都应该在任何正常的 RDBMS 中工作.

Both should work in any sane RDBMS.

这篇关于如果 Column1 不为空,则按 Column1 排序,否则按 Column2 排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆