日期:2014-05-18  浏览次数:20480 次

有字段,A,B,C,D,想要查询表中字段A,B值重复的行,怎么写哦???
就是要提取表中A,B字段的值都相同的行

------解决方案--------------------
create table T(ID int, code varchar(10), SpotName varchar(10), Name varchar(10))
insert T select 1, 'A ', '张三 ', 'SAD '
union all select 2, 'A ', '李四 ', 'RG '
union all select 3, 'B ', '王五 ', 'YTJ '
union all select 4, 'A ', '张三 ', 'IIO '
union all select 5, 'C ', '孙二麻子 ', 'ILL '
union all select 6, 'D ', '王五 ', '654 '

select * from T as A
where (select count(*) from T where code=A.code and SpotName=A.SpotName)> 1

--result
ID code SpotName Name
----------- ---------- ---------- ----------
1 A 张三 SAD
4 A 张三 IIO

(2 row(s) affected)
------解决方案--------------------
借marco08(天道酬勤) ( ) 的数据

select * from T a where exists(select * from T where code=a.code and SpotName=a.SpotName and id <> a.id)