让我们首先创建一个表-mysql> create table DemoTable
(
ProductAmount int,
PurchaseDate datetime
);
注意-假设当前日期为2010-09-15。
使用插入命令在表中插入一些记录-mysql> insert into DemoTable values(567,'2019-09-10');
mysql> insert into DemoTable values(1347,'2019-09-14');
mysql> insert into DemoTable values(2033,'2019-09-13');
mysql> insert into DemoTable values(1256,'2019-09-11');
mysql> insert into DemoTable values(1000,'2019-09-16');
使用select语句显示表中的所有记录-mysql> select *from DemoTable;
这将产生以下输出-+---------------+---------------------+
| ProductAmount | PurchaseDate |
+---------------+---------------------+
| 567 | 2019-09-10 00 :00 :00 |
| 1347 | 2019-09-14 00:00 :00 |
| 2033 | 2019-09-13 00:00 :00 |
| 1256 | 2019-09-11 00:00 :00 |
| 1000 | 2019-09-16 00:00 :00 |
+---------------+---------------------+
5 rows in set (0.00 sec)
以下是now()在MySQL查询中使用功能的查询-mysql> select sum(ProductAmount) from DemoTable
where PurchaseDate > NOW()- interval 3 day;
这将产生以下输出-+--------------------+
| sum(ProductAmount) |
+--------------------+
| 4380 |
+--------------------+
1 row in set (0.00 sec)