중복을 선택하려면 하위 쿼리를 사용할 수 있습니다. 먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.87 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name) values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Name) values('Mike'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name) values('Mike'); Query OK, 1 row affected (0.12 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select *from DemoTable;
출력
+----+--------+ | Id | Name | +----+--------+ | 1 | John | | 2 | Chris | | 3 | John | | 4 | David | | 5 | Bob | | 6 | Chris | | 7 | Mike | | 8 | Robert | | 9 | Mike | +----+--------+ 9 rows in set (0.00 sec)
다음은 모든 중복을 선택하는 쿼리입니다 -
mysql> select *from DemoTable -> where Name in (select Name from DemoTable group by Name having count(*) > 1);
출력
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Chris | | 3 | John | | 6 | Chris | | 7 | Mike | | 9 | Mike | +----+-------+ 6 rows in set (0.09 sec)