如何创建一个从日期开始的月份列,并显示您发现重复日期的相应列的总和?
为此,可以在MySQL中使用DATE_FORMAT()。让我们首先创建一个表-
create table DemoTable -> ( -> PurchaseDate date, -> Amount int -> );
使用插入命令在表中插入一些记录-
insert into DemoTable values('2019-10-12',500); insert into DemoTable values('2018-10-12',1000); insert into DemoTable values('2019-01-10',600); insert into DemoTable values('2018-10-12',600); insert into DemoTable values('2018-11-10',800);
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+--------------+--------+ | PurchaseDate | Amount | +--------------+--------+ | 2019-10-12 | 500 | | 2018-10-12 | 1000 | | 2019-01-10 | 600 | | 2018-10-12 | 600 | | 2018-11-10 | 800 | +--------------+--------+ 5 rows in set (0.00 sec)
这是从日期创建一个月列的查询,并显示一些相应的列,其中您发现重复的日期-
select sum(Amount) as Amount,date_format(PurchaseDate,'%b') AS Month from DemoTable -> group by date_format(PurchaseDate,'%Y-%m');
这将产生以下输出-
+--------+-------+ | Amount | Month | +--------+-------+ | 500 | Oct | | 1600 | Oct | | 600 | Jan | | 800 | Nov | +--------+-------+ 4 rows in set (0.00 sec)