外连接中的 AND 语法?
这是我发现有问题的更大查询的一小部分。
除了主键之外,我还尝试向外部联接添加一个过滤器。问题是我收到的结果的 StatusCode 不是 0 或 1,就好像它忽略了过滤器一样。
SELECT *
FROM Products P
LEFT OUTER JOIN OrderDetails OD ON OD.SkuNum = P.SkuNum
LEFT OUTER JOIN OrderHeader OH ON (OD.ShipmentNum = OH.ShipmentNum
AND (OH.StatusCode = 0 OR OH.StatusCode = 1))
WHERE P.SkuNum = XXXX
请注意,如果我将该语句 (OH.StatusCode = 0 OR OH.StatusCode = 1) 放在 where 子句中,它将按该条件过滤整个结果集,这也不是我想要的。
对于这种简单的英语加入,我想说“给我所有产品和其他这里未列出的东西。如果有任何该产品的发货,请向我提供它们的所有详细信息,其中货件状态为 1 或 0”
是我的语法错误还是我遗漏了什么?谢谢。
编辑:更新了查询以包含产品,以便更清楚地了解我正在寻找的内容并修复了换位错误。
this is a small piece of a much larger query that I have discovered is a problem.
I'm trying to add a filter to my outer join in addition to the primary key. The problem is that I'm receiving results that have an StatusCode other than 0 or 1, as if it is ignoring the filter.
SELECT *
FROM Products P
LEFT OUTER JOIN OrderDetails OD ON OD.SkuNum = P.SkuNum
LEFT OUTER JOIN OrderHeader OH ON (OD.ShipmentNum = OH.ShipmentNum
AND (OH.StatusCode = 0 OR OH.StatusCode = 1))
WHERE P.SkuNum = XXXX
Note that if I put that statement (OH.StatusCode = 0 OR OH.StatusCode = 1) in the where clause it filters the entire result set by that criteria which is not what I want either.
For this join in plain english, I'm trying to say "Give me all products and other stuff not listed here. If there are any shipments for this product, give me all the details for them where the shipment has a status of 1 or 0"
Is my syntax wrong or am I missing something? Thanks.
Edit: Updated the query to include products to make it clearer what I'm looking for and fixed a transposition error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
通常,对于给定的 OrderDetail,您将始终拥有一个 OrderHeader,但您可能没有状态为 0、1 的 OrderHeader。
通过执行左连接,您将获得该特定 Sku 的所有信息,然后如果订单标头没有状态0、1,这些列将为
NULL
。我认为你想要一个INNER JOIN。但是,这通常相当于将所有内容都放在 WHERE 中,所以我确信您已经完全描述了您想要在这里得到的内容。
看到您的编辑后,请尝试以下操作:
Typically you will always have an OrderHeader for a given OrderDetail, but you might not have one with status 0, 1.
By doing a left join, you are getting all for that particular Sku, and then if the order header doesn't have status 0, 1, those columns will be
NULL
.I would think you want an INNER JOIN. But then that would usually be equivalent to putting everything in the WHERE, so I'm note sure you're fully describing what you want to get here.
After seeing your edit, try this:
通过执行左连接,您是说您想要 skunum = xxxx 的所有订单详细信息,无论订单标头表中是否有任何匹配项。
根据您的查询和简单的英语描述,对我来说,听起来您想要内部联接。但也许我理解不够或者信息不够。内部联接只会返回发货状态为 1 或 0 并且 skunum 匹配的所有订单详细信息。
By doing a left join, you are saying you want all order details where skunum = xxxx regardless on if there is any matches in the order header table.
Based on your query and plain english description, to me it sounds like you want an inner join. But maybe I don't understand enough or have enough information. The inner join would only return all of the order details where the shipment has a status of 1 or 0 and the skunum matches.
这里不需要派生表。
通过将
ON OD.SkuNum = P.SkuNum
子句放在最后,产品上的LEFT JOIN
逻辑上会发生在最后。You don't need a derived table here.
By placing the
ON OD.SkuNum = P.SkuNum
clause last theLEFT JOIN
on Products logically happens last.不确定这是否是您要寻找的:
您的简单英语陈述似乎表明您正在寻找特定的货物。您可能需要将shipmentnum 添加到where 子句中。
Not sure if this is what you're looking for:
Your statement in plain english seems to idicate that you're looking for a specific shipment. You might want to add a shipmentnum to the where clause.
试试这个:
Try this: