이를 위해 GROUP BY와 함께 집계 함수 SUM()을 사용합니다. 먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable1522 -> ( -> ProductPurchaseDate date, -> NumberOfProduct int -> ); Query OK, 0 rows affected (1.51 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable1522 values('2019-01-21',45); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1522 values('2018-12-31',78); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1522 values('2019-01-21',67); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1522 values('2019-03-01',56); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1522 values('2018-01-21',97); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1522 values('2019-01-21',47); Query OK, 1 row affected (0.15 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select * from DemoTable1522;
이것은 다음과 같은 출력을 생성합니다 -
+---------------------+-----------------+ | ProductPurchaseDate | NumberOfProduct | +---------------------+-----------------+ | 2019-01-21 | 45 | | 2018-12-31 | 78 | | 2019-01-21 | 67 | | 2019-03-01 | 56 | | 2018-01-21 | 97 | | 2019-01-21 | 47 | +---------------------+-----------------+ 6 rows in set (0.00 sec)
다음은 MySQL에서 날짜에 따라 열을 계산하는 쿼리입니다 -
mysql> select ProductPurchaseDate,sum(NumberOfProduct) from DemoTable1522 -> group by ProductPurchaseDate;
이것은 다음과 같은 출력을 생성합니다 -
+---------------------+----------------------+ | ProductPurchaseDate | sum(NumberOfProduct) | +---------------------+----------------------+ | 2019-01-21 | 159 | | 2018-12-31 | 78 | | 2019-03-01 | 56 | | 2018-01-21 | 97 | +---------------------+----------------------+ 4 rows in set (0.00 sec)