按日期对结果进行分组并显示重复值计数的 MySQL 查询?
我们首先创建一个表格 -
mysql> create table DemoTable1496 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> PassengerCode varchar(20), -> ArrivalDate datetime -> ); Query OK, 0 rows affected (0.85 sec)
使用插入命令向表格中插入一些记录 -
mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('202','2013-03-12 10:12:34'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('202_John','2013-03-12 11:00:00'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('204','2013-03-12 10:12:34'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('208','2013-03-14 11:10:00'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('202','2013-03-18 12:00:34'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('202','2013-03-18 04:10:01'); Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表格中的所有记录 -
mysql> select * from DemoTable1496;
这将产生以下输出 -
+----+---------------+---------------------+ | Id | PassengerCode | ArrivalDate | +----+---------------+---------------------+ | 1 | 202 | 2013-03-12 10:12:34 | | 2 | 202_John | 2013-03-12 11:00:00 | | 3 | 204 | 2013-03-12 10:12:34 | | 4 | 208 | 2013-03-14 11:10:00 | | 5 | 202 | 2013-03-18 12:00:34 | | 6 | 202 | 2013-03-18 04:10:01 | +----+---------------+---------------------+ 6 rows in set (0.00 sec)
这是按日期对结果进行分组并显示重复值计数的查询 -
mysql> select date(ArrivalDate),count(ArrivalDate) from DemoTable1496 -> where PassengerCode like concat('%','202','%') -> group by date(ArrivalDate);
这将产生以下输出 -
+-------------------+--------------------+ | date(ArrivalDate) | count(ArrivalDate) | +-------------------+--------------------+ | 2013-03-12 | 2 | | 2013-03-18 | 2 | +-------------------+--------------------+ 2 rows in set (0.00 sec)
广告