要查找重复的行,请在MySQL中使用GROUP BY和HAVING。让我们首先创建一个表-
mysql> create table DemoTable1494 -> ( -> ShippingDate datetime, -> CustomerCountryName varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1494 values('2018-09-10 12:34:50','US'); mysql> insert into DemoTable1494 values('2019-09-10 12:34:50','AUS'); mysql> insert into DemoTable1494 values('2018-09-10 11:00:00','US'); mysql> insert into DemoTable1494 values('2017-12-31 01:10:40','UK'); mysql> insert into DemoTable1494 values('2019-09-10 05:00:50','AUS');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1494;
这将产生以下输出-
+---------------------+---------------------+ | ShippingDate | CustomerCountryName | +---------------------+---------------------+ | 2018-09-10 12:34:50 | US | | 2019-09-10 12:34:50 | AUS | | 2018-09-10 11:00:00 | US | | 2017-12-31 01:10:40 | UK | | 2019-09-10 05:00:50 | AUS | +---------------------+---------------------+ 5 rows in set (0.00 sec)
以下是在MySQL中查找重复行的查询-
mysql> select ShippingDate,CustomerCountryName,count(*) as c -> from DemoTable1494 -> group by date(ShippingDate),CustomerCountryName -> having c >=2 -> order by c;
这将产生以下输出-
+---------------------+---------------------+---+ | ShippingDate | CustomerCountryName | c | +---------------------+---------------------+---+ | 2018-09-10 12:34:50 | US | 2 | | 2019-09-10 12:34:50 | AUS | 2 | +---------------------+---------------------+---+ 2 rows in set (0.00 sec)