MySQL에서 날짜를 그룹화하려면 GROUP BY 절을 사용하십시오 -
mysql> create table DemoTable2002 ( CustomerName varchar(20), CustomerShippingDate datetime ); Query OK, 0 rows affected (0.61 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable2002 values('Chris','2019-01-10'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2002 values('David','2018-12-31'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2002 values('David','2019-12-16'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2002 values('Chris','2018-12-01'); Query OK, 1 row affected (0.12 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select * from DemoTable2002;
이것은 다음과 같은 출력을 생성합니다 -
+--------------+----------------------+ | CustomerName | CustomerShippingDate | +--------------+----------------------+ | Chris | 2019-01-10 00:00:00 | | David | 2018-12-31 00:00:00 | | David | 2019-12-16 00:00:00 | | Chris | 2018-12-01 00:00:00 | +--------------+----------------------+ 4 rows in set (0.00 sec)
날짜를 그룹화하여 레코드를 표시하는 쿼리는 다음과 같습니다. -
mysql> select date(min(CustomerShippingDate)),date(max(CustomerShippingDate)),CustomerName from DemoTable2002 group by CustomerName;
이것은 다음과 같은 출력을 생성합니다 -
+---------------------------------+---------------------------------+--------------+ | date(min(CustomerShippingDate)) | date(max(CustomerShippingDate)) | CustomerName | +---------------------------------+---------------------------------+--------------+ | 2018-12-01 | 2019-01-10 | Chris | | 2018-12-31 | 2019-12-16 | David | +---------------------------------+---------------------------------+--------------+ 2 rows in set (0.00 sec)