带有 WHERE 子句的 LEFT OUTER JOIN
我有两张桌子。 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 JOIN
s. Is there a way around this, or am I looking for something else entirely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试:
因此将
AND WO_BreakerRail.Date = @Date
添加到连接上Try:
Thus adding
AND WO_BreakerRail.Date = @Date
onto the join谓词条件需要位于连接的
On
子句中,而不是位于 where 子句中。外连接的工作方式是在分析连接条件之后,将“外侧”中与内侧不匹配的所有行添加回......但这一切都发生在处理 where 子句之前。因此,如果 where 子句谓词从外连接的外侧过滤属性,所有这些行将再次被删除......(它们都是空)。将谓词放在连接条件中,然后在将缺失的行添加回之前对其进行评估...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...您可以使用此 where 子句:
You could use this where clause:
在
LEFT OUTER JOIN
语句上添加WO_BreakerRail.Date = @Date
,而不是WHERE
:或者您可以将其添加到 WHERE:
add the
WO_BreakerRail.Date = @Date
on theLEFT OUTER JOIN
statement and not theWHERE
:or you can add it to the WHERE:
您想要的是将您的
WO_BreakerRail
过滤到所需的日期,然后将其左联接到indRailType
。如果您只添加 WHERE 语句,则会以相反的方式完成,从而导致您所描述的问题。到目前为止提供的解决方案应该可以工作(将条件移动到 LEFT JOIN 中),但我想建议一种替代方案:您可以使用子查询来指定应该完成事情的顺序:(
未经测试,但据我知道,SQL Server CE 支持子查询。)
What you want is to filter your
WO_BreakerRail
to the desired date before LEFT JOINing it toindRailType
. 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:
(Untested, but as far as I know, SQL Server CE supports subqueries.)
或者您可以将其添加到
WHERE
:or you can add it to the
WHERE
: