仅显示在MySQL中出现至少三次的重复列名
COUNT()
为此使用HAVING。让我们首先创建一个表-
mysql> create table DemoTable1351 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(40) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1351(StudentName) values('Chris'); mysql> insert into DemoTable1351(StudentName) values('Bob'); mysql> insert into DemoTable1351(StudentName) values('Bob'); mysql> insert into DemoTable1351(StudentName) values('David'); mysql> insert into DemoTable1351(StudentName) values('Bob'); mysql> insert into DemoTable1351(StudentName) values('David'); mysql> insert into DemoTable1351(StudentName) values('Bob'); mysql> insert into DemoTable1351(StudentName) values('Mike'); mysql> insert into DemoTable1351(StudentName) values('David');
使用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)