Computer >> 컴퓨터 >  >> 프로그램 작성 >> MySQL

날짜를 계산하고 반복되는 날짜도 가져오는 MySQL 쿼리

<시간/>

개수를 표시하려면 집계 함수 COUNT(*)를 사용합니다. 먼저 테이블을 생성하겠습니다 -

mysql> create table DemoTable1321
-> (
-> ArrivalDatetime timestamp
-> );
Query OK, 0 rows affected (0.50 sec)

예시

삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -

mysql> insert into DemoTable1321 values(now());
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1321 values('2019-01-10 12:34:00');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1321 values('2019-06-12 11:34:00');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1321 values('2019-06-12 04:50:00');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1321 values('2019-09-18 10:50:45');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable1321 values('2019-06-12 06:10:20');
Query OK, 1 row affected (0.11 sec)

select 문을 사용하여 테이블의 모든 레코드 표시 -

mysql> select * from DemoTable1321;

출력

+---------------------+
| ArrivalDatetime     |
+---------------------+
| 2019-09-18 21:49:17 |
| 2019-01-10 12:34:00 |
| 2019-06-12 11:34:00 |
| 2019-06-12 04:50:00 |
| 2019-09-18 10:50:45 |
| 2019-06-12 06:10:20 |
+---------------------+
6 rows in set (0.00 sec)

예시

다음은 날짜를 계산하는 쿼리입니다 -

mysql> select date(ArrivalDatetime) as OnlyDate,count(*) as Total from DemoTable1321
-> group by date(ArrivalDatetime)
-> order by date(ArrivalDatetime);

출력

+------------+-------+
| OnlyDate   | Total |
+------------+-------+
| 2019-01-10 | 1    |
| 2019-06-12 | 3    |
| 2019-09-18 | 2    |
+------------+-------+
3 rows in set (0.00 sec)