在 MySQL 中查找重复的列值并显示它们
为此,使用 GROUP BY HAVING 子句。让我们首先创建一个表 -
mysql> create table DemoTable1858 ( ModelNumber varchar(50) ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable1858 values('Audi A4'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1858 values('Audi A6'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1858 values('Audi A4'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1858 values('Audi Q5'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1858 values('Audi R8'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1858 values('Audi Q5'); Query OK, 1 row affected (0.00 sec)
使用 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)
广告