PHP,MYSQL:按日期排序,但空日期最后不是第一个 [英] PHP, MYSQL: Order by date but empty dates last not first

查看:27
本文介绍了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.

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

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