让我们首先创建一个表-
mysql> create table DemoTable1526 -> ( -> CustomerName varchar(20), -> PurchaseDate date -> );
使用insert命令在表中插入一些记录。在这里,我们插入了2019年的日期-
mysql> insert into DemoTable1526 values('Adam','2019-06-01'); mysql> insert into DemoTable1526 values('Sam','2019-04-26'); mysql> insert into DemoTable1526 values('Chris','2019-05-24'); mysql> insert into DemoTable1526 values('David','2019-10-10'); mysql> insert into DemoTable1526 values('Bob','2019-12-31');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1526;
这将产生以下输出-
+--------------+--------------+ | CustomerName | PurchaseDate | +--------------+--------------+ | Adam | 2019-06-01 | | Sam | 2019-04-26 | | Chris | 2019-05-24 | | David | 2019-10-10 | | Bob | 2019-12-31 | +--------------+--------------+ 5 rows in set (0.00 sec)
当前日期如下-
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2019-10-08 | +------------+ 1 row in set (0.00 sec)
这是要在2019年的最后3个月之前订购的查询,然后按字母顺序-
mysql> select * from DemoTable1526 -> order by PurchaseDate > curdate() - interval 3 month desc,CustomerName;
这将产生以下输出-
+--------------+--------------+ | CustomerName | PurchaseDate | +--------------+--------------+ | Bob | 2019-12-31 | | David | 2019-10-10 | | Adam | 2019-06-01 | | Chris | 2019-05-24 | | Sam | 2019-04-26 | +--------------+--------------+ 5 rows in set (0.00 sec)