在MySQL中计算整月和整年代金券价值总计
其中,使用MySQL中的MONTH()和YEAR()方法。让我们首先创建一个表−
mysql> create table DemoTable1562
-> (
-> VoucherValue int,
-> RechargeDate date
-> );
Query OK, 0 rows affected (1.40 sec)
使用insert命令向表中插入一些记录−
mysql> insert into DemoTable1562 values(149,'2019-10-21');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1562 values(199,'2019-10-13');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1562 values(399,'2018-10-13');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable1562 values(450,'2019-10-13');
Query OK, 1 row affected (0.20 sec)
使用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)
阅读更多:MySQL 教程