怎么利用SQL语句查询数据库中具体某个字段的重复行
可用group by……having来实现。
可做如下测试:
1、创建表插入数据:
create table test(id int,name varchar(10))insert into test values (1,'张三')insert into test values (2,'李四')insert into test values (3,'张三')insert into test values (4,'王五')insert into test values (5,'赵六')其中name是张三的有两行,也就是重复行。
2、执行sql语句如下:
select * from test where name in (select name from test group by name having COUNT(*)>1)结果如图:
如何用一条SQL语句查询数据库重复记录
方法如下:
select * from 你的表名
a where id=(select min(id) from 你的表名 whereitem_id=a.item_id)
在查询之前先把数据库表中的第一行复制到sid里在去,然后让sid和下面的每一行进行比较
取所有相同的行的最小的一下,也可以取最大的,结果是一样的。
这样让所有的行都比较不就得到不重复的数据了。