select col1,col2,... from table1 group by col1,col2,... having count(*)>1
select 所有列的列表 from 你的表 group by 所有列的列表 having count(*)>1
偷懒的方法就是用动态生成SQL的方法:declare @sql varchar(8000) set @sql='' select @sql=@sql+',['+name +']' from syscolumns where object_id('你的表')=id set @sql=right(@sql,len(@sql)-1) exec('select '+@sql+' from 你的表 group by '+@sql+' having count(*)>1')
请问楼上,动态生成sql的方法为什么是偷懒的呢,能否解释一下
就是呀,怎么动态生成SQL的方法?麻烦讲解一下吧!
我的偷懒的意思是不用一个一个的去写字段名.declare @sql varchar(8000) set @sql='' --下面这句就是从系统表中获取指定表中的所有字段名. select @sql=@sql+',['+name +']' from syscolumns where object_id('你的表')=id set @sql=right(@sql,len(@sql)-1) exec('select '+@sql+' from 你的表 group by '+@sql+' having count(*)>1')
select col1,col2... from tableName group by col1,col2 ... having sum(1)>1
select id,coll,col2 from table t1 where id in(select t2.id from table t2 t2.id=t1.id)
现在有一个问题,表A里有B,C,D字段。 怎样查出B字段重复的记录,要求查询的结果显示所有的字段。
呵呵,此楼先借小乙一用。 To yzf111(一台电脑,一包烟,一个火机) : 我是说在同一个表中.好象子查询还少了个Where select id,coll,col2 from table t1 where id in(select t2.id from table t2 WHERE t2.id=t1.id)
group by col1,col2,...
having count(*)>1
from 你的表
group by 所有列的列表
having count(*)>1
set @sql=''
select @sql=@sql+',['+name +']' from syscolumns where object_id('你的表')=id
set @sql=right(@sql,len(@sql)-1)
exec('select '+@sql+' from 你的表 group by '+@sql+' having count(*)>1')
set @sql=''
--下面这句就是从系统表中获取指定表中的所有字段名.
select @sql=@sql+',['+name +']' from syscolumns where object_id('你的表')=id
set @sql=right(@sql,len(@sql)-1)
exec('select '+@sql+' from 你的表 group by '+@sql+' having count(*)>1')
怎样查出B字段重复的记录,要求查询的结果显示所有的字段。
To yzf111(一台电脑,一包烟,一个火机) :
我是说在同一个表中.好象子查询还少了个Where
select id,coll,col2 from table t1 where id in(select t2.id from table t2 WHERE t2.id=t1.id)