SQL Server:左外连接上的条件(IF-ELSE)

时间:2017-03-28 13:49:24

标签: sql sql-server join left-join

我有一个场景,我想在一个连接上放一个条件,即如果A = B然后加入1 set,另一个加入另一个。两种情况下的表格相同,只是条件不同。我尝试过使用大小写,但是我遇到了语法错误。

SELECT * 
FROM [Table1] CUP
LEFT OUTER JOIN Table2 CP ON CUP.StateCode = CP.StateCode 
                          AND (CUP.ClientId = CP.ClientId OR CP.ClientId = 0)

我尝试了什么

SELECT * 
FROM [Table1] CUP
LEFT OUTER JOIN Table2 CP
          ON CASE
                WHEN CUP.ClientId =  CP.ClientId 
                   THEN CUP.StateCode = CP.StateCode AND CUP.ClientId =  CP.ClientId
                   ELSE CUP.StateCode = CP.StateCode AND (CUP.ClientId <>  CP.ClientId or CP.ClientId = 0)

@juan

 ON 
   CUP.StateCode = CP.StateCode AND ((CUP.ClientId =  CP.ClientId or CP.ClientId <> 0) OR (CUP.ClientId <> CP.ClientId or CP.ClientId = 0))

4 个答案:

答案 0 :(得分:1)

  • 您必须使用coalesce作为SELECT条款
  • 请注意CP1.ClientId is null删除了Gordon解决方案有关NULLS的限制
select  ...

from                    table1  CUP

        left outer join Table2  CP1

        on              CP1.StateCode = CUP.StateCode  
                    and CP1.ClientId  = CUP.ClientId 

        left outer join Table2  CP2

        on              CP1.ClientId is null

                    and CP2.StateCode = CUP.StateCode 
                    and CP2.ClientId  = 0

答案 1 :(得分:0)

我写了这个:

SELECT * 
  FROM   TABLE1 CUP
  INNER JOIN TABLE2 CP ON CUP.StateCode = CP.StateCode 
   AND CUP.ClientId =  CP.ClientId   
  UNION ALL
  SELECT * 
  FROM (SELECT * FROM TABLE1 WHERE NOT EXISTS (SELECT 1 FROM TABLE2 WHERE TABLE2.CLIENTID = TABLE1.CLIENTID) ) CUP 
  INNER JOIN TABLE2 CP  ON    CUP.StateCode = CP.StateCode AND CP.CLIENTID=0

我用这些数据试了一下:

CREATE TABLE TABLE1  (STATECODE INT, CLIENTID INT);
CREATE TABLE TABLE2  (STATECODE INT, CLIENTID INT);
INSERT INTO TABLE1 VALUES (1,100);
INSERT INTO TABLE1 VALUES (1,200);
INSERT INTO TABLE1 VALUES (4,300);
INSERT INTO TABLE1 VALUES (5,400);
INSERT INTO TABLE1 VALUES (4,500);

INSERT INTO TABLE2 VALUES (1,100);
INSERT INTO TABLE2 VALUES (1,200);
INSERT INTO TABLE2 VALUES (4,0);
INSERT INTO TABLE2 VALUES (5,0);

输出:

STATECODE   CLIENTID    STATECODE   CLIENTID
----------- ----------- ----------- -----------
1           100         1           100
1           200         1           200
4           300         4           0
5           400         5           0
4           500         4           0

答案 2 :(得分:0)

我认为你想要两个left join,一个是第一个值,第二个是默认值:

SELECT CUP.*, COALESCE(CP.col1, CPdef.col1) as col1, . . .
FROM [Table1] CUP LEFT OUTER JOIN
     Table2 CP
     ON CUP.StateCode = CP.StateCode AND CUP.ClientId = CP.ClientId LEFT OUTER JOIN
     Table2 CdPef
     ON CUP.StateCode = CPdef .StateCodeCP AND CPdef.ClientId = 0;

请注意,COALESCE()假定您关注的列不是NULL天。

如果您只需要一次匹配,则另一种方法是outer apply

SELECT CUP.*, cp.*
FROM [Table1] CUP OUTER APPLY
     (SELECT top 1 cp.*
      FROM Table2 Cp
      WHERE CUP.StateCode = CP.StateCode AND
            (CUP.ClientId = CP.ClientId OR CP.ClientId = 0)
      ORDER BY CP.ClientId DESC  -- assumes `CP.ClientId` is not null
     ) cp;

实际上,您可以使用TOP (1) WITH TIES来获得多个匹配项。所以这不仅限于一个匹配的行。

答案 3 :(得分:0)

当一个案例全部说完并完成时,它以一个值结束。

case when SomeColumn = 4 then 1 end

如您所见,返回值为VALUE

您可以使用它来返回连接中的列,但实际上您要返回列

中的值
select aa.a
from aa
inner join bb
on aa.a = case when bb.a is null then 4 else bb.a end

您不能使用案例来返回表达式

case when aa.a = 4 then (aa.a = bb.a) end -- this is gonna fail

在这些情况下使用OR:

SELECT * 
FROM [Table1] CUP
LEFT OUTER JOIN Table2 CP
  ON (CUP.ClientId =  CP.ClientId 
      and CUP.StateCode = CP.StateCode )
  or (CUP.StateCode = CP.StateCode 
      AND (CUP.ClientId <>  CP.ClientId 
      or CP.ClientId = 0))