使用时间戳和枚举进行排序的 MySQL 查询?
为此可使用 ORDER BY DATE()。我们首先创建一个表。此表中有一列为 DATE 类型,另一列为 ENUM 类型−
mysql> create table DemoTable -> ( -> JoiningDate date, -> Status ENUM('Good','Excellent','Bad') -> ); Query OK, 0 rows affected (0.58 sec)
使用 insert 命令向表中插入部分记录−
mysql> insert into DemoTable values('2019-01-21','Excellent'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Status) values('Bad'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Status) values('Good'); Query OK, 1 row affected (0.13 sec)
使用 select 语句显示表中的所有记录−
mysql> select *from DemoTable;
将生成以下输出−
+-------------+-----------+ | JoiningDate | Status | +-------------+-----------+ | 2019-01-21 | Excellent | | NULL | Bad | | NULL | Good | +-------------+-----------+ 3 rows in set (0.00 sec)
以下是按时间戳和枚举排序的查询−
mysql> select *from DemoTable -> order by DATE(JoiningDate) ASC, Status asc;
将生成以下输出−
+-------------+-----------+ | JoiningDate | Status | +-------------+-----------+ | NULL | Good | | NULL | Bad | | 2019-01-21 | Excellent | +-------------+-----------+ 3 rows in set (0.00 sec)
广告