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.

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.

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.)

Thanks!

推荐答案

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;

Mysql相关问答推荐

MySQL 8.0.28覆盖(和函数)索引未使用

如何有效地计算共同的朋友/追随者?

更新MySQL表中子记录的序号

我是否需要在 N+1 列上建立索引,才能有效地在 N 列上执行 SELECT,并按其他列排序?

MySQL match against给出奇怪的结果

将 Go var 传递给 MySQL - try 在 Go lang 程序 (MySQL db) 中执行 db.Exec 时出错

如何查询由另一个查询创建的表?

相关查询 - 在派生表中,我可以有多个嵌套级别吗?

MySQL过滤食谱上的多对多 - 成分表

MySQL REGEXP 在没有 BINARY 模式的情况下区分大小写?

mysql 5.5;可以从记录中排除表吗?

使用 MySQL 在树中查找 Leaf

MySQL - 计算行重复的最大计数

为什么 ORDER BY 'id' 'desc' 不返回语法错误?

Spring Boot:Jdbc javax.net.ssl.SSLException:在接收对等方的 close_notify 之前关闭入站

MySQL - 表'my_table'没有被锁定表锁定

外键可以引用非唯一索引吗?

加快 mysql 转储和导入

使用 mysqldriver 连接数据库时出错

什么是mysql的BETWEEN性能超过..?