用于计算日期并获取重复日期的 MySQL 查询


要显示计数,请使用聚合函数 COUNT(*)。我们首先创建一个表 -

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

示例

使用 insert 命令在表中插入一些记录 -

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)

更新时间: 08-Jul-2020

193 次浏览

开启你的 职业生涯

完成课程以获得认证

开始
广告