可用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)
结果如图:
假设有张基础表 EMP 里面有sal,id,ename 等等 然后要查工资sal 都是3000的雇员信息 可以这样写:
select id,ename,sal from emp group by sal having count(sal)>1;
就能查出工资sal字段 重复的所有职员信息了!!
我一般用这个:
假设怀疑重复的字段名为SeriNo,
select * from [tablename]
group by SeriNo
having count(SeriNo)<>1
select count(1) as 出现次数,setname,setnum from table_name
where datediff(DAY,subtime,getdate())<=30
group by setname,setnum having count(1)>1
select * from tabel1 where filed01 in (select filed01 fromtabel1
group by filed01
having count(filed01 )>1)
filed01 为有重复字段的列