PHP,MYSQL:按日期排序,但空日期最后不是第一个
我从 MySQL 获取一个包含待办事项标题和截止日期的数组.我想按日期订购,并把最旧的放在上面.但是有些待办事项没有日期.这些待办事项我不想显示在第一个位置,而是显示在列表的底部.不幸的是 MySQL 把空的放在第一位.
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.
有什么方法可以在一个查询中完成(不能使用 MySQLi,使用 CI 的 ActiveRecord).我可以对所有没有日期的待办事项进行第二次查询,并将它们放在底部.但我想在一个查询中完成——如果可能的话?
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?
推荐答案
您可以在 MySQL 中使用 ORDER BY
子句来实现.首先按 NULL
排序,然后按日期排序.
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
注意:假设没有日期的行是NULL
.
相关文章