PHP,MYSQL:按日期排序,但空日期不会先于 [英] PHP, MYSQL: Order by date but empty dates last not first

查看:307
本文介绍了PHP,MYSQL:按日期排序,但空日期不会先于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从MySQL获取Todo标题和到期日期的数组。我想按日期订购,并且最旧的。但有一些todos没有约会。这些todos我不想在第一个位置显示,而是在我的列表的底部。不幸的是MySQL首先放空了。



有没有办法我可以在一个查询中执行(不能使用MySQLi,使用CI的ActiveRecord)。我可以运行第二个查询所有todos没有日期,并将它们放在底部。但是我想在一个查询中进行一次查询 - 如果可能的话

解决方案

你可以在MySQL中使用 ORDER BY 子句。首先排序 NULL ,然后是日期。

  SELECT * FROM your_table ORDER BY(date_column IS NULL),date_column ASC 

注意:没有日期的行是 NULL


I fetch an array with todo titles and due dates from MySQL. I want to order it by date and have the oldest on top. But there are some todos without a date. These todos I don't want to show at first positions but rather at the bottom of my list. Unfortunately MySQL put the empty ones first.

Is there any way I can do it in one query (can't use MySQLi, using CI's ActiveRecord). I could run a second query for all todos without dates and put them at the bottom. But I'd like to make it in one query – if possible?

解决方案

You can do it in MySQL with the ORDER BY clause. Sort by NULL first, then the date.

SELECT * FROM your_table ORDER BY (date_column IS NULL), date_column ASC

Note: This assumes rows without a date are NULL.

这篇关于PHP,MYSQL:按日期排序,但空日期不会先于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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