SQL Query从两个表返回不匹配的行

时间:2016-10-01 09:37:16

标签: sql sql-server sql-server-2012

我有2个表tblEmp1和tblEmp2,我需要通过比较tblEmp2从中提取非匹配的tblEmp1行。

从tblEmp1

中选择*
EmpMasterID     DeptID  MngrID      TEMPID      Salary1         Salary2         DOJ         
46              3       995         559         1000000000      1000000000      2016-12-31      
47              3       999         556         2500000000      2500000000      2016-12-31      
48              3       994         165         1500000000      1500000000      2016-12-31          
49              3       998         566         7500000000      7500000000      2016-12-31          
50              3       1049        562         9500000000      9500000000      2016-12-31

从tblEmp2

中选择*
EmpID   DeptName    DeptID  MngrID  TEMPID      Salary1         Salary2         DOJ                         
84      XYZ         3       994     165         1500000000      1500000000      2016-12-31  
85      XYZ         3       995     559         1000000000      1000000000      2016-12-31      
86      XYZ         3       999     556         2500000000      2500000000      2016-12-31

正如你们上面所看到的DeptID = 3,MngrID = 994,995,999和TempID = 165,559,556我有匹配的salary1和salary2与DOJ =' 2016-12-31'

我的要求是我应该只从第一个表中提取那些在tblEmp2表中不存在的DFeptID = 3和DOJ =' 2016-12-31' 即,我的查询应返回tblEmp1表

中的结尾2行
EmpMasterID     DeptID  MngrID      TEMPID      Salary1         Salary2         DOJ                     
49              3       998         566         7500000000      7500000000      2016-12-31          
50              3       1049        562         9500000000      9500000000      2016-12-31  

因为在tblEmp2表中它们不存在,其中DeptID = 3且DOJ =' 2016-12-31'

这就是我到目前为止所做的事情

select t1.EmpMasterID, t1.DeptID, t1.MngrID, t1.TEMPID, t1.Salary1, t1.Salary2, t1.DOJ  
from tblEmp1 as t1
left outer join tblEmp2 as t2
on  t1.DeptID = t2.DeptID, t1.MngrID = t2.MngrID, t1.TEMPID = t2.TEMPID, t1.DOJ = t2.DOJ
where t1.DeptID = 3 and t1.DOJ = '2016-12-31' and t2.MngrID is null and t2.TEMPID is null

重要提示:我忘记提到tblEmp2表我需要根据DeptName进行过滤...即,tblEmp1.DeptID = 3,tblEmp2.DeptName =' XYZ'和tblEmp1.DOJ =' 2016-12-31'应该是搜索/过滤条件。

3 个答案:

答案 0 :(得分:0)

尝试使用以下脚本。在AND条件下使用AND运算符更改','。

   SELECT t1.EmpMasterID, t1.DeptID, t1.MngrID, t1.TEMPID, t1.Salary1, t1.Salary2, t1.DOJ  
   FROM tblEmp1 as t1
       left outer join tblEmp2 as t2
          on  t1.DeptID = t2.DeptID and t1.MngrID = t2.MngrID and t1.TEMPID = t2.TEMPID and t1.DOJ = t2.DOJ
            AND    t2.DeptName = 'XYZ' -- AND t1.Salary1=t2.Salary1 AND t1.Salary2=t2.Salary2 --(include these columns if you wanted to compare salary)
   WHERE t1.DeptID = 3 and t1.DOJ = '2016-12-31' 
         and t2.DeptId is null

或使用 NOT EXISTS ..

 SELECT t1.EmpMasterID, t1.DeptID, t1.MngrID, t1.TEMPID, t1.Salary1, t1.Salary2, t1.DOJ  
   FROM tblEmp1 as t1
       WHERE NOT EXISTS(SELECT 1
                        FROM  tblEmp2 as t2
                        WHERE  t1.DeptID = t2.DeptID and t1.MngrID = t2.MngrID and t1.TEMPID = t2.TEMPID and t1.DOJ = t2.DOJ 
                            -- AND t1.Salary1=t2.Salary1 AND t1.Salary2=t2.Salary2 --(include these columns if you wanted to compare salary)
                                 )
       AND t1.DeptID = 3 and t1.DOJ = '2016-12-31' 

答案 1 :(得分:0)

select  *
from    tblEmp1 t1
join    (

    select  DeptID, MngrID, TempID
    from    tblEmp1
except
    select  DeptID, MngrID, TempID
    from    tblEmp2
) t2 on t1.DeptId = t2.DeptId and t1. MngrId = t2.MngrId and t1.TempID = t2.TempID

答案 2 :(得分:0)

我跑了这个:

;WITH tblEmp1 AS (
SELECT *
FROM (VALUES
(46,              3,       995,         559,         1000000000,      1000000000,      '2016-12-31'),
(47,              3,       999,         556,         2500000000,      2500000000,      '2016-12-31'),
(48,              3,       994,         165,         1500000000,      1500000000,      '2016-12-31'),       
(49,              3,       998,         566,         7500000000,      7500000000,      '2016-12-31'),
(50,              3,       1049,        562,         9500000000,      9500000000,      '2016-12-31')
) as t(EmpMasterID,DeptID,MngrID,TEMPID,Salary1,Salary2,DOJ)
), tblEmp2 AS (
SELECT *
FROM (VALUES
(84,      'XYZ',         3,       994,     165,         1500000000,      1500000000,      '2016-12-31'),
(85,      'XYZ',         3,       995,     559,         1000000000,      1000000000,      '2016-12-31'),
(86,      'XYZ',         3,       999,     556,         2500000000,      2500000000,      '2016-12-31')
) as t(EmpID,DeptName,DeptID,MngrID,TEMPID,Salary1,Salary2,DOJ )
)

SELECT e1.*
FROM tblEmp1 e1
LEFT JOIN tblEmp2 e2
    ON e1.DeptID = e2.DeptID 
        AND e1.MngrID = e2.MngrID
        AND e1.TEMPID = e2.TEMPID
        AND e1.DOJ = e2.DOJ
where e1.DeptID = 3 and e1.DOJ = '2016-12-31'
    AND e2.MngrID IS NULL AND e2.TEMPID IS NULL

得到这个:

EmpMasterID DeptID  MngrID  TEMPID  Salary1     Salary2     DOJ
49          3       998     566     7500000000  7500000000  2016-12-31
50          3       1049    562     9500000000  9500000000  2016-12-31

您需要的行,与您发布的查询几乎相同。什么假设是一个问题?