在MySQL中查找重复的列值并显示它们
为此,请使用GROUPBYHAVING子句。让我们首先创建一个表-
mysql> create table DemoTable1858 ( ModelNumber varchar(50) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1858 values('Audi A4'); mysql> insert into DemoTable1858 values('Audi A6'); mysql> insert into DemoTable1858 values('Audi A4'); mysql> insert into DemoTable1858 values('Audi Q5'); mysql> insert into DemoTable1858 values('Audi R8'); mysql> insert into DemoTable1858 values('Audi Q5');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1858;
这将产生以下输出-
+-------------+ | ModelNumber | +-------------+ | Audi A4 | | Audi A6 | | Audi A4 | | Audi Q5 | | Audi R8 | | Audi Q5 | +-------------+ 6 rows in set (0.00 sec)
这是在MySQL中查找重复列值的查询-
mysql> select ModelNumber from DemoTable1858 group by ModelNumber having count(ModelNumber) >=2;
这将产生以下输出-
+-------------+ | ModelNumber | +-------------+ | Audi A4 | | Audi Q5 | +-------------+ 2 rows in set (0.00 sec)