이를 위해 HAVING COUNT()를 사용하십시오. 먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable1351 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(40) -> ); Query OK, 0 rows affected (1.08 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable1351(StudentName) values('Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1351(StudentName) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1351(StudentName) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1351(StudentName) values('David'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1351(StudentName) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1351(StudentName) values('David'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1351(StudentName) values('Bob'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1351(StudentName) values('Mike'); Query OK, 1 row affected (0.68 sec) mysql> insert into DemoTable1351(StudentName) values('David'); Query OK, 1 row affected (0.12 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select * from DemoTable1351;
이것은 다음과 같은 출력을 생성합니다 -
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Chris | | 2 | Bob | | 3 | Bob | | 4 | David | | 5 | Bob | | 6 | David | | 7 | Bob | | 8 | Mike | | 9 | David | +-----------+-------------+ 9 rows in set (0.00 sec)
다음은 적어도 세 번 나타나는 중복 열 이름을 표시하는 쿼리입니다 -
mysql> select * from DemoTable1351 -> group by StudentName -> having count(StudentName) >=3;
이것은 다음과 같은 출력을 생성합니다 -
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 2 | Bob | | 4 | David | +-----------+-------------+ 2 rows in set (0.00 sec)