MySQL查询按当前日期和月份排序?
为此,您可以使用ORDERBYCASE语句。让我们首先创建一个表-
create table DemoTable ( DueDate date );
注意-假设当前日期为2019-07-22。
使用插入命令在表中插入一些记录-
insert into DemoTable values('2019-03-10');
insert into DemoTable values('2019-08-25');
insert into DemoTable values('2019-06-01');
insert into DemoTable values('2019-12-31');使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+------------+ | DueDate | +------------+ | 2019-03-10 | | 2019-08-25 | | 2019-06-01 | | 2019-12-31 | +------------+ 4 rows in set (0.00 sec)
以下是按日期和月份排序的查询-
select *from DemoTable order by case when DueDate > curdate() then 0 else 1 end, DueDate;
这将产生以下输出-
+------------+ | DueDate | +------------+ | 2019-08-25 | | 2019-12-31 | | 2019-03-10 | | 2019-06-01 | +------------+ 4 rows in set (0.00 sec)
热门推荐
10 文艺结婚祝福语短语简短
11 好朋友夫妻祝福语简短
12 元旦联谊祝福语简短精辟
13 婚庆花篮祝福语简短大气
14 简短大气的狗年祝福语
15 50多岁生日简短祝福语
16 金店立秋祝福语简短英文
17 对生活祝福语简短精辟
18 38节贺卡简短祝福语