为此,请使用MySQLMONTH()
和YEAR()
方法。让我们首先创建一个表-
mysql> create table DemoTable1562 -> ( -> VoucherValue int, -> RechargeDate date -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1562 values(149,'2019-10-21'); mysql> insert into DemoTable1562 values(199,'2019-10-13'); mysql> insert into DemoTable1562 values(399,'2018-10-13'); mysql> insert into DemoTable1562 values(450,'2019-10-13');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1562;
这将产生以下输出-
+--------------+--------------+ | VoucherValue | RechargeDate | +--------------+--------------+ | 149 | 2019-10-21 | | 199 | 2019-10-13 | | 399 | 2018-10-13 | | 450 | 2019-10-13 | +--------------+--------------+ 4 rows in set (0.00 sec)
当前日期如下-
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2019-10-13 | +------------+ 1 row in set (0.00 sec)
这是查询自月和年开始以来的凭证总值的查询。
mysql> select sum(VoucherValue) from DemoTable1562 -> where month(RechargeDate)=month(curdate()) -> and year(RechargeDate)=year(curdate());
这将产生以下输出-
+-------------------+ | sum(VoucherValue) | +-------------------+ | 798 | +-------------------+ 1 row in set (0.00 sec)