PHP, MYSQL: Order by date but empty dates last not first(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:按日期排序,但空日期最后不是第一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP,MYSQL:按日期排序,但空日期最后不是第一个
基础教程推荐
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
