带有 WHERE 子句的 LEFT OUTER JOIN

发布于 2024-08-25 20:44:43 字数 997 浏览 7 评论 0原文

我有两张桌子。 indRailType 包含与 ID 值配对的名称列表,我在其他表中使用该值来指示轨道类型。 WO_BreakerRail 包含一个日期列和一个铁路代码列,对应于 indRailType 中的相同代码以及一些其他数据。 WO_BreakerRail 中针对每个日期的每种铁路类型上的任何活动都有一行。因此,我可以有 3 行日期为 3/19/2010,每行表示不同的铁路代码以及发生的情况。

当我使用以下LEFT OUTER JOIN时,我得到一个包含所有类型铁路的表,其中 19 日没有发生任何事情的行中包含空值。现在,这只是有效的,因为我的 WO_BreakerRail 表中现在只有一个日期,即 19 日。当我添加更多具有不同日期的行时,事情就会变得混乱。

这是我的 SQL 语句,它现在给出了我想要的结果:

SELECT WO_BreakerRail.ID, indRailType.RailType, WO_BreakerRail.CreatedPieces, 
    WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged, 
    WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop
FROM indRailType
LEFT OUTER JOIN WO_BreakerRail 
    ON indRailType.RailCode = WO_BreakerRail.RailCode

现在,当我添加 WHERE WO_BreakerRail.Date = @Date 子句时,我会丢失 JOIN 中的所有行 什么也没发生。我不想要这样。从阅读来看,听起来我想要的是 FULL OUTER JOIN ,但是 SQL Server Compact Edition 不支持 FULL OUTER JOIN 。有没有办法解决这个问题,或者我正在寻找其他的东西?

I have two tables. indRailType contains a list of the names paired with an ID value that I use in other tables to indicate the rail type. WO_BreakerRail contains a date column and a rail code column that corresponds to the same code in indRailType and some other data. There's a row in WO_BreakerRail for any activity on each rail type, for every date. So I could have 3 rows dated for 3/19/2010, each row indicates a different rail code, and what happened.

When I use the following LEFT OUTER JOIN, I get a table with all the types of rail, with nulls in the rows where nothing happened on the 19th. Now, this is only working because I only have one date represented in my WO_BreakerRail table right now, the 19th. When I add more rows with different dates, things will go haywire.

This is my SQL statement, which right now gives me exactly the results I want:

SELECT WO_BreakerRail.ID, indRailType.RailType, WO_BreakerRail.CreatedPieces, 
    WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged, 
    WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop
FROM indRailType
LEFT OUTER JOIN WO_BreakerRail 
    ON indRailType.RailCode = WO_BreakerRail.RailCode

Now, when I add in a WHERE WO_BreakerRail.Date = @Date clause I lose all the rows in the JOIN which nothing happened. I don't want that. From reading up, it sounds like a FULL OUTER JOIN is what I want, but SQL Server Compact Edition doesn't support FULL OUTER JOINs. Is there a way around this, or am I looking for something else entirely?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

拥抱没勇气 2024-09-01 20:44:43

尝试:

SELECT WO_BreakerRail.ID, indRailType.RailType, WO_BreakerRail.CreatedPieces, 
    WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged,
    WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop
FROM indRailType
LEFT OUTER JOIN WO_BreakerRail ON indRailType.RailCode = WO_BreakerRail.RailCode 
            AND WO_BreakerRail.Date = @Date

因此将 AND WO_BreakerRail.Date = @Date 添加到连接上

Try:

SELECT WO_BreakerRail.ID, indRailType.RailType, WO_BreakerRail.CreatedPieces, 
    WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged,
    WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop
FROM indRailType
LEFT OUTER JOIN WO_BreakerRail ON indRailType.RailCode = WO_BreakerRail.RailCode 
            AND WO_BreakerRail.Date = @Date

Thus adding AND WO_BreakerRail.Date = @Date onto the join

节枝 2024-09-01 20:44:43

谓词条件需要位于连接的 On 子句中,而不是位于 where 子句中。外连接的工作方式是在分析连接条件之后,将“外侧”中与内侧不匹配的所有行添加回......但这一切都发生在处理 where 子句之前。因此,如果 where 子句谓词从外连接的外侧过滤属性,所有这些行将再次被删除......(它们都是空)。将谓词放在连接条件中,然后在将缺失的行添加回之前对其进行评估...

SELECT b.ID, t.RailType, b.CreatedPieces, 
       b.OutsideSource, b.Charged, b.Rejected, 
       b.RejectedToCrop 
FROM indRailType t
   LEFT JOIN WO_BreakerRail b
        ON t.RailCode = b.RailCode 
            And b.Date = @Date

The predicate condition needs to be in the On clause for the join, not in the where clause. The way Outer joins work, is after the join conditions are analyzed, all the rows from the "outer side" that do not match the inner side are added back in.... But this all happens before the where clause is processed. So if the where clause predicate filters on an attribute from the outer side of an outer join, all those rows will be removed again... (They are all null). Put the predicate in the join condition instead, then it will get evaluated before the missing rows are added back in...

SELECT b.ID, t.RailType, b.CreatedPieces, 
       b.OutsideSource, b.Charged, b.Rejected, 
       b.RejectedToCrop 
FROM indRailType t
   LEFT JOIN WO_BreakerRail b
        ON t.RailCode = b.RailCode 
            And b.Date = @Date
终遇你 2024-09-01 20:44:43

您可以使用此 where 子句:

WHERE WO_BreakerRail.Date = @Date OR WO_BreakerRail.Date IS NULL

You could use this where clause:

WHERE WO_BreakerRail.Date = @Date OR WO_BreakerRail.Date IS NULL
橪书 2024-09-01 20:44:43

LEFT OUTER JOIN 语句上添加 WO_BreakerRail.Date = @Date,而不是 WHERE

SELECT
    WO_BreakerRail.ID, indRailType.RailType, WO_BreakerRail.CreatedPieces, WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged, WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop
    FROM indRailType 
        LEFT OUTER JOIN WO_BreakerRail ON indRailType.RailCode = WO_BreakerRail.RailCode AND WO_BreakerRail.Date = @Date

或者您可以将其添加到 WHERE:

SELECT
    WO_BreakerRail.ID, indRailType.RailType, WO_BreakerRail.CreatedPieces, WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged, WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop
    FROM indRailType 
        LEFT OUTER JOIN WO_BreakerRail ON indRailType.RailCode = WO_BreakerRail.RailCode
    WHERE (WO_BreakerRail.Date = @Date OR WO_BreakerRail.Date IS NULL)

add the WO_BreakerRail.Date = @Date on the LEFT OUTER JOIN statement and not the WHERE:

SELECT
    WO_BreakerRail.ID, indRailType.RailType, WO_BreakerRail.CreatedPieces, WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged, WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop
    FROM indRailType 
        LEFT OUTER JOIN WO_BreakerRail ON indRailType.RailCode = WO_BreakerRail.RailCode AND WO_BreakerRail.Date = @Date

or you can add it to the WHERE:

SELECT
    WO_BreakerRail.ID, indRailType.RailType, WO_BreakerRail.CreatedPieces, WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged, WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop
    FROM indRailType 
        LEFT OUTER JOIN WO_BreakerRail ON indRailType.RailCode = WO_BreakerRail.RailCode
    WHERE (WO_BreakerRail.Date = @Date OR WO_BreakerRail.Date IS NULL)
执手闯天涯 2024-09-01 20:44:43

您想要的是将您的 WO_BreakerRail 过滤到所需的日期,然后将其左联接到 indRailType。如果您只添加 WHERE 语句,则会以相反的方式完成,从而导致您所描述的问题。

到目前为止提供的解决方案应该可以工作(将条件移动到 LEFT JOIN 中),但我想建议一种替代方案:您可以使用子查询来指定应该完成事情的顺序:(

SELECT R.ID, indRailType.RailType, R.CreatedPieces, R.OutsideSource, R.Charged, R.Rejected, R.RejectedToCrop
  FROM indRailType LEFT OUTER JOIN
       (SELECT * FROM WO_BreakerRail WHERE Date = @Date) R
       ON indRailType.RailCode = R.RailCode

未经测试,但据我知道,SQL Server CE 支持子查询。)

What you want is to filter your WO_BreakerRail to the desired date before LEFT JOINing it to indRailType. If you just add the WHERE statement, it is done the other way around, which results in the problem you described.

The solutions provided so far should work (move the condition into the LEFT JOIN), but I'd like to suggest an alternative: You can use a subquery to specify the order in which things should be done:

SELECT R.ID, indRailType.RailType, R.CreatedPieces, R.OutsideSource, R.Charged, R.Rejected, R.RejectedToCrop
  FROM indRailType LEFT OUTER JOIN
       (SELECT * FROM WO_BreakerRail WHERE Date = @Date) R
       ON indRailType.RailCode = R.RailCode

(Untested, but as far as I know, SQL Server CE supports subqueries.)

心清如水 2024-09-01 20:44:43

或者您可以将其添加到 WHERE

SELECT WO_BreakerRail.ID, indRailType.RailType, 
  WO_BreakerRail.CreatedPieces, 
  WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged, 
  WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop 
FROM indRailType, WO_BreakerRailCode 
WHERE indRailType.RailCode = WO_BreakerRail.RailCode(+) 
 AND WO_BreakerRail.Date (+) =@Date 

or you can add it to the WHERE:

SELECT WO_BreakerRail.ID, indRailType.RailType, 
  WO_BreakerRail.CreatedPieces, 
  WO_BreakerRail.OutsideSource, WO_BreakerRail.Charged, 
  WO_BreakerRail.Rejected, WO_BreakerRail.RejectedToCrop 
FROM indRailType, WO_BreakerRailCode 
WHERE indRailType.RailCode = WO_BreakerRail.RailCode(+) 
 AND WO_BreakerRail.Date (+) =@Date 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文