获取MySQL中两个给定日期之间的记录总和
为此,请使用BETWEEN关键字。让我们首先创建一个-
create table DemoTable1444 -> ( -> Value int, -> PurchaseDate datetime -> );
使用insert在表中插入一些记录-
insert into DemoTable1444 values(40,'2019-01-10'); insert into DemoTable1444 values(100,'2019-10-03'); insert into DemoTable1444 values(170,'2019-11-21'); insert into DemoTable1444 values(70,'2018-12-05');
使用选择显示表中的所有记录-
select * from DemoTable1444;
这将产生以下输出-
+-------+---------------------+ | Value | PurchaseDate | +-------+---------------------+ | 40 | 2019-01-10 00:00:00 | | 100 | 2019-10-03 00:00:00 | | 170 | 2019-11-21 00:00:00 | | 70 | 2018-12-05 00:00:00 | +-------+---------------------+ 4 rows in set (0.00 sec)
以下是查询以获取两个给定日期之间的总和-
select sum(Value) from DemoTable1444 where PurchaseDate between '2019-10-02' and '2019-12-31';
这将产生以下输出-
+------------+ | sum(Value) | +------------+ | 270 | +------------+ 1 row in set (0.02 sec)