当前位置: 代码迷 >> Sql Server >> 求重复记录查询的SQL话语,
  详细解决方案

求重复记录查询的SQL话语,

热度:57   发布时间:2016-04-27 11:41:15.0
求重复记录查询的SQL语句,急!
表格式如下:
id name pay
1 aaa 1
2 aaa 2
3 ccc 3
4 ddd 2
5 ccc 5
6 eee 3
7 aaa 3
8 ddd 2

如果name相同,而pay不一致,则返回对应的数据;

即返回的记录是 ID=1,2,3,5,7的数据

------解决方案--------------------
SQL code
SELECT ID FROM TABLE AS TWHERE EXISTS (SELECT * FROM TABLE WHERE T.NAME = NAME AND T.PAY <> PAY)
------解决方案--------------------
贴错了运行结果,
SQL code
IF OBJECT_ID('[test]') IS NOT NULL DROP TABLE [test]GO CREATE TABLE [test]([id] int,[name] varchar(10),[pay] int)insert into [test]select 1,'aaa',1 union allselect 2,'aaa',2 union allselect 3,'ccc',3 union allselect 4,'ddd',2 union allselect 5,'ccc',5 union allselect 6,'eee',3 union allselect 7,'aaa',3 union allselect 8,'ddd',2select * from [test] twhere exists ( select 1 from [test] where t.[name]=[name] and t.[pay]<>[pay])/*(8 row(s) affected)id          name       pay----------- ---------- -----------1           aaa        12           aaa        23           ccc        35           ccc        57           aaa        3(5 row(s) affected)*/