【问题标题】:Select rows from two joined table that not in a third table从两个连接表中选择不在第三个表中的行
【发布时间】:2017-11-20 13:06:25
【问题描述】:

我有三张桌子:

表 1: (EmployeeID int, EmployeeType int)

表 2: (AllowanceID int, EmployeeType int)

表 3: (EmployeeID int, AllowanceID int)

我需要一个查询来从两个表(表 1、表 2)中选择未生成第三个表(表 3)的连接行。

我试过了:

Select t1.EmployeeID, t2.AllowanceID
From Table2 t2 Inner Join
     Table1 t1
     on t1.EmployeeType = t2.EmployeeType
where Not Exists (select 1
                  From Table3 t3 
                  where t3.EmployeeID = t1.EmployeeID and 
                        t3.AllowanceID = t2.AllowanceID
                 )

但没有成功。

CREATE TABLE [dbo].[Table1](
    [EmployeeID] [int] NULL,
    [EmployeeType] [tinyint] NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[Table2](
    [AllowanceID] [int] NOT NULL,
    [EmployeeType] [int] NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[Table3](
    [AllowanceID] [int] NULL,
    [EmployeeID] [int] NULL
) ON [PRIMARY]


Insert into Table1 (EmployeeID,EmployeeType)
Values
(352395,    10),
(352396,    16),
(352397,    15),
(352398,    10),
(3523991,   16),
(NULL,  16)

Insert into Table2 (AllowanceID, EmployeeType)
Values
(100,   50),
(30,    9),
(32,    10),
(37,    16),
(512,   28),
(6000,  10)

Insert into Table3 (AllowanceID,EmployeeID)
Values
(NULL,  352400),
(32,    NULL),
(37,    NULL),
(37,    352395),
(6000,  352395),
(30,    352396),
(32,    352396),
(37,    352396),
(512,   352396),
(6000,  352396),
(30,    352397),
(32,    352397),
(37,    352397),
(512,   352397),
(6000,  352397),
(30,    352398),
(32,    352398),
(37,    352398),
(512,   352398),
(6000,  352398),
(30,    352399),
(32,    352399),
(37,    352399),
(512,   352399),
(6000,  352399)

【问题讨论】:

  • “没有成功”是什么意思?你能提供样本数据和期望的结果吗?
  • 我同意 Gordon Linoff 的请求。作为说明,我编辑了您的帖子以在您的代码中添加一些换行符(没有更改代码本身的任何内容)。事实上,它延伸得很远,用户必须滚动很多才能看到所有内容。只是对未来帖子格式的一些反馈。
  • 您发布的代码对我来说似乎很好,但没有示例数据(如 DDL+DML)、清晰的问题描述和期望的结果,很难回答您的问题。
  • 请以文本而非图像的形式提供示例数据。

标签: sql sql-server join


【解决方案1】:

如果您使用的是 SQL SERVER,请使用以下查询

    SELECT t1.empoyeeId, t2.AllowanceId
FROM table1 t1
INNER JOIN table2 t2 ON t2.EmployeeTypeId = t1.EmployeeTypeID 
EXCEPT 
SELECT employeeID , allowanceID
FROM table2 

【讨论】:

    【解决方案2】:
    SELECT t1.EmployeeID, t2.AllowanceID
    FROM Table1 t1
    JOIN Table2 t2 ON t1.EmployeeType = t2.EmployeeType
    LEFT JOIN Table3 t3 ON t3.AllowanceID = t2.AllowanceID AND t3.EmployeeID = 
    t1.EmployeeID
    WHERE t3.EmployeeID IS NULL
    

    【讨论】:

    • 请添加一些文字来解释您的答案
    【解决方案3】:

    也许你可以试试,除了,使用顺序会改变结果

    SELECT t1.EmployeeID , t2.AllowanceID
    FROM Table2 t2
    INNER JOIN Table1 t1 ON t1.EmployeeType = t2.EmployeeType
    EXCEPT
    SELECT t3.EmployeeID , t3.AllowanceID
    FROM Table3 t3
    ;
    
    SELECT t3.EmployeeID , t3.AllowanceID
    FROM Table3 t3
    EXCEPT
    SELECT t1.EmployeeID , t2.AllowanceID
    FROM Table2 t2
    INNER JOIN Table1 t1 ON t1.EmployeeType = t2.EmployeeType
    

    返回查询左侧的任何不同值,但除外 也不是从正确的查询返回的运算符。 (ms docs)

    【讨论】:

      【解决方案4】:

      您可以尝试使用带有所需表的子查询和不希望找到记录的表的左连接:

      Select A.EmployeeID, A.AllowanceID
      FROM (SELECT t1.EmployeeID, t2.AllowanceID
      From Table2 t2 Inner Join
           Table1 t1
           on t1.EmployeeType = t2.EmployeeType ) A
         LEFT JOIN t3 ON  t3.EmployeeID = A.EmployeeID and 
                          t3.AllowanceID = A.AllowanceID
      WHERE t3.EmployeeID IS NULL and t3.AllowanceID IS NULL
      

      【讨论】:

        【解决方案5】:

        您的查询的问题在于它期望null 等于nullwhich is not how null works

        要更正此问题,只需将 t3.EmployeeID = t1.EmployeeID 更改为 (t3.EmployeeID = t1.EmployeeID or t1.EmployeeId is null)

        查询变为:

        select 
            t1.EmployeeID
          , t2.AllowanceID
        from Table2 t2
          inner join Table1 t1
            on t1.EmployeeType = t2.EmployeeType
        where not exists (
          select 1
          from Table3 t3
          where (t3.EmployeeID  = t1.EmployeeID or t1.EmployeeId is null)
            and t3.AllowanceID = t2.AllowanceID
          )
        

        rextester 演示:http://rextester.com/OPRHN52820

        +------------+-------------+
        | EmployeeID | AllowanceID |
        +------------+-------------+
        |     352395 |          32 |
        |    3523991 |          37 |
        +------------+-------------+
        

        【讨论】:

        • 完美..谢谢
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多