可以使用SQL按日期排序,但将空日期放在结果集的后面吗? [英] Possible to use SQL to sort by date but put null dates at the back of the results set?

查看:402
本文介绍了可以使用SQL按日期排序,但将空日期放在结果集的后面吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL数据库中有一堆任务,其中一个字段是截止日期".并非每个任务都必须有截止日期.

I have a bunch of tasks in a MySQL database, and one of the fields is "deadline date". Not every task has to have to a deadline date.

我想使用SQL按截止日期对任务进行排序,但是将没有截止日期的任务放在结果集中.现在,首先显示空日期,然后按照最早的截止日期对其余日期进行最早的排序.

I'd like to use SQL to sort the tasks by deadline date, but put the ones without a deadline date in the back of the result set. As it is now, the null dates show up first, then the rest are sorted by deadline date earliest to latest.

是否有关于仅使用SQL做到这一点的任何想法? (我可以根据需要使用PHP来做到这一点,但是仅使用SQL的解决方案就可以了.)

Any ideas on how to do this with SQL alone? (I can do it with PHP if needed, but an SQL-only solution would be great.)

谢谢!

推荐答案

这是仅使用标准SQL而不使用ISNULL()的解决方案.该功能不是标准的SQL,并且可能不适用于其他品牌的RDBMS.

Here's a solution using only standard SQL, not ISNULL(). That function is not standard SQL, and may not work on other brands of RDBMS.

SELECT * FROM myTable
WHERE ...
ORDER BY CASE WHEN myDate IS NULL THEN 1 ELSE 0 END, myDate;

这篇关于可以使用SQL按日期排序,但将空日期放在结果集的后面吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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