简体   繁体   English

左外连接的SQL where子句

[英]SQL where clause for left outer join

I have a problem with a view I want to create. 我对要创建的视图有疑问。 I have two tables joined in a left outer join, say tableA and tableB , where tableB is left outer joined. 我在左外连接中连接了两个表,比如tableAtableB ,其中tableB是外连接的。

I want to select only those rows from table B where state equals 4, so I add WHERE state = 4 to my query. 我想只选择表B中状态等于4的那些行,所以我将WHERE state = 4添加到我的查询中。 Now the result set is trimmed quite a bit because all rows without a matching row in tableB are removed from the result (since state isn't 4 for those rows). 现在,因为没有匹配的行所有行的结果集进行修剪颇有几分tableB是从结果中删除(因为状态不是4对那些行)。 I also tried WHERE state = 4 OR state IS NULL , doesn't work either (since state technically isn't NULL when there is no state). 我也试过WHERE state = 4 OR state IS NULL ,不工作或者(因为state技术上是不是NULL的时候没有状态)。

So what I need is a WHERE statement which is only evaluated when there actually is a row, does such a thing exist? 所以我需要的是一个WHERE语句,它仅在实际存在行时进行求值,是否存在这样的事情?

If not I see two options: join (SELECT * FROM tableB WHERE state = 4) instead of table B, or create a view with the same WHERE statement and join that instead. 如果没有,我看到两个选项: join (SELECT * FROM tableB WHERE state = 4)而不是表B,或者创建一个具有相同WHERE语句的视图并将其连接起来。 What's the best option performance wise? 明智的选择是什么?

This is SQL Server 2008 R2 by the way. 顺便说一句,这是SQL Server 2008 R2。

You put the conditions in the on clause. 您将条件放在on子句中。 Example: 例:

select a.this, b.that
from TableA a
left join TableB b on b.id = a.id and b.State = 4

You can add state = 4 to the join condition. 您可以将state = 4添加到连接条件。

select * 
from T1
  left outer join T2
    on T1.T1ID = T2.T1ID and
       T2.state = 4

Even easier than a subquery is expanding the on clause, like; 比子查询更容易扩展on子句,比如;

select  *
from    TableA a
left    join
        TableB b
on      a.b_id = b.id
        and b.state = 4

All rows from TableA will appear, and only those from TableB with state 4. 将显示TableA中的所有行,并且仅显示具有状态4的TableB中的行。

SQL Server will probably execute the view, expanded on , and subquery in exactly the same way. SQL Server将可能执行的观点,扩大on以完全相同的方式,和子查询。 So performance wise, there should be little difference. 所以表现明智,应该没什么区别。

Alternative approach: (1) inner join to table B where state equals 4, (2) antijoin to table B to find rows that don't exist, (3) union the results: 替代方法:(1)内部连接到表B,其中状态等于4,(2)反连接到表B以查找不存在的行,(3)结合结果:

SELECT A1.ID, A1.colA, B1.ColB
  FROM tableA AS A1
       INNER JOIN tableB AS B1
          ON A1.ID = B1.ID
             AND B1.state = 4
UNION
SELECT A1.ID, A1.colA, '{{MISSING}}' AS ColB
  FROM tableA AS A1
 WHERE NOT EXISTS (
                   SELECT * 
                     FROM tableB AS B1
                     WHERE A1.ID = B1.ID
                  );

Alternatively: 或者:

SELECT A1.ID, A1.colA, B1.ColB
  FROM tableA AS A1
       JOIN tableB AS B1
          ON A1.ID = B1.ID
             AND B1.state = 4
UNION
SELECT ID, colA, '{{NA}}' AS ColB
  FROM tableA 
 WHERE ID IN (
              SELECT ID
                FROM tableA
              EXCEPT 
              SELECT ID
                FROM tableB
             );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM