我想只检索一个表中的ID号,而不是另一个表中的ID号

时间:2015-04-08 09:08:12

标签: sql

下面是表1

select  
    nContractPk 
from 
    CustomerPayment 
where 
    nContractPk in (select id            
                    from Contract 
                    where id in (SELECT nContractPk
                                 FROM CustomerPayment
                                 GROUP BY nContractPk
                                 HAVING COUNT(CASE WHEN bPaid <> 1 THEN 1 END) = 0 
                                    AND COUNT(CASE WHEN bPaid = 1 THEN 1 END) > 1) 
                                    AND nStatus = 10)

这是第二张表:

select  
    nContractPk 
from
    EmployeePayment  
where 
    nContractPk in (select id    
                    from Contract 
                    where id in (SELECT nContractPk
                                 FROM EmployeePayment
                                 GROUP BY nContractPk
                                 HAVING COUNT(CASE WHEN bPaid <> 1 THEN 1 END) = 0 
                                    AND COUNT(CASE WHEN bPaid = 1 THEN 1 END) > 1) 
                                    AND nStatus = 10) 

我想在customerpayment中仅检索id退出,但不在employeeepayment中检索

请帮忙解决这个问题。

提前致谢

1 个答案:

答案 0 :(得分:0)

尝试在两个表之间进行LEFT连接,并从另一个表中过滤掉null,如下所示:

SELECT c.nContractPk
FROM CustomerPayment c LEFT JOIN EmployeePayment e
ON c.nContractPk = e.nContractPk
WHERE e.nContractPk IS NULL