중복 행을 찾으려면 MySQL에서 GROUP BY 및 HAVING을 사용하십시오. 먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable1494 -> ( -> ShippingDate datetime, -> CustomerCountryName varchar(20) -> ); Query OK, 0 rows affected (0.63 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable1494 values('2018-09-10 12:34:50','US'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1494 values('2019-09-10 12:34:50','AUS'); Query OK, 1 row affected (0.65 sec) mysql> insert into DemoTable1494 values('2018-09-10 11:00:00','US'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1494 values('2017-12-31 01:10:40','UK'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1494 values('2019-09-10 05:00:50','AUS'); Query OK, 1 row affected (0.49 sec)
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)