左加入和条件不给出期望的结果?

时间:2016-02-09 08:49:32

标签: sql sql-server join where-clause

我不知道如何解释这个问题。但请阅读说明。 我有两张表,如下所示,

EntityProgress表 -

 EntityID       CurrentStateID
   101             1154
   201             1155
   301             1155

EnityApprovar表

 EntityID        StateID        ActorID
   201             1154            8
   201             1154            9
   201             1155            8
   301             1154            8
   301             1154            9
   301             1155            9

现在我想要的是如果我将ActorID=2作为参数传递,那么它应该只返回一行,如下所示,因为我们在entityapprovar表中没有任何匹配的enityID。

   EntityID      CurrentStateID     
    101              1154

但如果我通过了ActorID=9那么它应该给我如下结果,

   EntityID      CurrentStateID
      301            1155

因为我们在EntityApprover表中有entityID匹配记录,并且对于那个entityID我们有currentstateID,为此我们将actorid设为9。

所以为了得到我已经完成的结果,

 SELECT
      E.EntityID,
      E.CurrentStateID
 FROM 
      EntityProgress E LEFT JOIN EntityApprover EP 
      ON E.EntityID = EP.EntityID AND E.CurrentStateID = EP.StateID
 WHERE
     -- some conditions
     AND ((ISNULL(EP.ActorID,0) )= 0 
           OR ((ISNULL(EP.ActorID,0))!= 0 AND EP.ActorID = @ActorID AND Ep.CurrentStateID = E.StateID))

BUt当我通过2时,我获得了第一个结果,但是当我通过9/8时,我得不到所需的结果。可能这很简单,但我坚持下去。我需要其他一些观点给我不同的方式。     如果感到困惑,请随意留下评论。

3 个答案:

答案 0 :(得分:1)

我试图回答你。

<强>查询

DECLARE @ActorID int = 2

DECLARE @EntityProgress table
(
    EntityID int,
    CurrentStateID int
)

DECLARE @EnityApprovar table
(
    EntityID int,
    StateID int,
    ActorID int
)

INSERT into @EntityProgress
    values (101, 1154),
            (201, 1155),
            (301, 1155)


INSERT into @EnityApprovar
    VALUES (201, 1154, 8),
            (201, 1154, 9),
            (201, 1155, 8),
            (301, 1154, 8),
            (301, 1154, 9),
            (301, 1155, 9)

SELECT
    E.EntityID
    ,E.CurrentStateID
    ,EP.ActorID
FROM @EntityProgress E
LEFT JOIN @EnityApprovar EP
    ON E.EntityID = EP.EntityID
    AND E.CurrentStateID = EP.StateID
WHERE ((EP.ActorID IS NULL AND NOT EXISTS (SELECT 1 FROM @EnityApprovar WHERE ActorID = @ActorID))
        OR (EP.ActorID = @ActorID))

当您通过@ActorID = 2时,它会给出以下输出。

EntityID    CurrentStateID  
101          1154         

当你通过@ActorID = 9时,它会给出以下输出。

EntityID    CurrentStateID
301         1155    

您想要的是预期的。

答案 1 :(得分:0)

您必须在查询中包含not exists,因为您在传入时试图排除的行9没有信息来确定表中是否有其他行匹配。

SELECT
      E.EntityID,
      E.CurrentStateID
 FROM 
      EntityProgress E LEFT JOIN EnityApprovar EP 
      ON E.EntityID = EP.EntityID AND E.CurrentStateID = EP.StateID
 WHERE ((ISNULL(EP.ActorID,0) = 0 
   and   not exists(select 1 
                      from EnityApprovar ep2 
                     where ep2.ActorID = @ActorID ))
    OR  (ISNULL(EP.ActorID,0) != 0 
   AND   EP.ActorID = @ActorID  
   AND   E.CurrentStateID = Ep.StateID))

答案 2 :(得分:-1)

您的ActorId查找表应该是您查询的起点。如果你的架构中没有(我怀疑),你可以尝试这个作为起点。使用T-SQL语法:

    DECLARE @ActorID int
    SET @ActorID = 2

    SELECT * FROM
         (SELECT @ActorID AS EntityId) 
    Actor
    LEFT JOIN
    EntityApprover EA
    ON Actor.EntityId = EA.EntityId ...

从那里你可以提取其他列值。

相关问题