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)
热门推荐
10 妹妹结婚伴娘祝福语简短
11 宝宝祝福语诗意简短 古文
12 运动社团祝福语简短英文
13 所有的新年祝福语简短
14 练钢琴的祝福语简短
15 新年的生日祝福语简短
16 写生日祝福语简短唯美
17 喜欢的人送礼祝福语简短
18 幽默的离职祝福语简短