PHP,MYSQL:按日期排序,但空日期不会先于 [英] PHP, MYSQL: Order by date but empty dates last not first
问题描述
有没有办法我可以在一个查询中执行(不能使用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屋!