MySQL查询以降序排列时间戳,但将时间戳记00:00:00放在第一位?
让我们首先创建一个表&mnus;
mysql> create table DemoTable ( `timestamp` timestamp );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(now()); mysql> insert into DemoTable values('00:00:00'); mysql> insert into DemoTable values('2018-01-10 12:34:45'); mysql> insert into DemoTable values('2019-12-31 10:50:45');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+---------------------+ | timestamp | +---------------------+ | 2019-08-17 06:17:12 | | 0000-00-00 00:00:00 | | 2018-01-10 12:34:45 | | 2019-12-31 10:50:45 | +---------------------+ 4 rows in set (0.00 sec)
以下是通过首先显示0时间戳以降序对时间戳进行排序的查询-
mysql> select *from DemoTable order by (`timestamp`=0) DESC,`timestamp` DESC;
这将产生以下输出-
+---------------------+ | timestamp | +---------------------+ | 0000-00-00 00:00:00 | | 2019-12-31 10:50:45 | | 2019-08-17 06:17:12 | | 2018-01-10 12:34:45 | +---------------------+ 4 rows in set (0.00 sec)