让我们首先创建一个表-
mysql> create table DemoTable1496 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> PassengerCode varchar(20), -> ArrivalDate datetime -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('202','2013-03-12 10:12:34'); mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('202_John','2013-03-12 11:00:00'); mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('204','2013-03-12 10:12:34'); mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('208','2013-03-14 11:10:00'); mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('202','2013-03-18 12:00:34'); mysql> insert into DemoTable1496(PassengerCode,ArrivalDate) values('202','2013-03-18 04:10:01');
使用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)