如何从日期创建列数并显示找到重复日期的相应列的和?
为此,可以在 MySQL 中使用 DATE_FORMAT()。我们先创建一个表 -
mysql> create table DemoTable -> ( -> PurchaseDate date, -> Amount int -> ); Query OK, 0 rows affected (0.52 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values('2019-10-12',500); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2018-10-12',1000); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-01-10',600); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2018-10-12',600); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2018-11-10',800); Query OK, 1 row affected (0.18 sec)
使用 select 语句从表中显示所有记录 -
mysql> 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)
以下是从日期创建列数并显示一些找到重复日期的相应列的查询 -
mysql> 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)
广告