日期:2014-05-17  浏览次数:20473 次

请教高手:如何查询同时满足二个条件
create table Detail
(
ID int,
contractNo varchar(50),
CustCode varchar(50),
Amount numeric(18,3)
)


insert into detail(ContractNo,CustCode,Amount) values('ds20111230','ws001',1000)
insert into detail(ContractNo,CustCode,Amount) values('ds20111230','ws002',1300)
insert into detail(ContractNo,CustCode,Amount) values('ds20111231','ws003',1000)
insert into detail(ContractNo,CustCode,Amount) values('ds20111232','ws002',1000)
insert into detail(ContractNo,CustCode,Amount) values('ds20111233','ws002',1000)
insert into detail(ContractNo,CustCode,Amount) values('ds20111234','ws001',1000)
insert into detail(ContractNo,CustCode,Amount) values('ds20111235','ws001',1000)

请高手指教如何查询: 要查询ContractNo='ds20111230' 所对应的CustCode和Amount同时重复的其它ContractNo
比如:ContractNo='ds20111230' 所对应的CustCode='ws001'和Amount=1000和
  CustCode='ws002'和Amount=1300


也就是说查询:CustCode='ws001'和Amount=1000重复的记录和CustCode='ws002'和Amount=1300重复的记录



   





------解决方案--------------------
SQL code

select *
from Detail t
where exists (select 1 from Detail where CustCode=t.CustCode and Amount=t.Amount and ContractNo='ds20111230')

------解决方案--------------------
探讨

SQL code

select *
from Detail t
where exists (select 1 from Detail where CustCode=t.CustCode and Amount=t.Amount and ContractNo='ds20111230')