外连接中的 AND 语法?

发布于 2024-12-03 11:28:08 字数 644 浏览 1 评论 0原文

这是我发现有问题的更大查询的一小部分。

除了主键之外,我还尝试向外部联接添加一个过滤器。问题是我收到的结果的 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 技术交流群。

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

发布评论

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

评论(6

美煞众生 2024-12-10 11:28:08

通常,对于给定的 OrderDetail,您将始终拥有一个 OrderHeader,但您可能没有状态为 0、1 的 OrderHeader。

通过执行左连接,您将获得该特定 Sku 的所有信息,然后如果订单标头没有状态0、1,这些列将为 NULL

我认为你想要一个INNER JOIN。但是,这通常相当于将所有内容都放在 WHERE 中,所以我确信您已经完全描述了您想要在这里得到的内容。

看到您的编辑后,请尝试以下操作:

SELECT * 
FROM Products P
LEFT JOIN (
    SELECT OD.SkuNum, OD.ShipmentNum, etc.
    FROM OrderDetails OD
    INNER JOIN OrderHeader OH
        ON OD.ShipmentNum = OH.ShipmentNum
            AND (OH.StatusCode = 0 OR OH.StatusCode = 1)
) AS Orders ON Orders.SkuNum = P.SkuNum
WHERE P.SkuNum = XXXX

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:

SELECT * 
FROM Products P
LEFT JOIN (
    SELECT OD.SkuNum, OD.ShipmentNum, etc.
    FROM OrderDetails OD
    INNER JOIN OrderHeader OH
        ON OD.ShipmentNum = OH.ShipmentNum
            AND (OH.StatusCode = 0 OR OH.StatusCode = 1)
) AS Orders ON Orders.SkuNum = P.SkuNum
WHERE P.SkuNum = XXXX
小伙你站住 2024-12-10 11:28:08

通过执行左连接,您是说您想要 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.

终陌 2024-12-10 11:28:08

这里不需要派生表。

通过将 ON OD.SkuNum = P.SkuNum 子句放在最后,产品上的 LEFT JOIN 逻辑上会发生在最后。

SELECT *
FROM   Products P
       LEFT OUTER JOIN OrderDetails OD
                       INNER JOIN OrderHeader OH
                         ON OD.ShipmentNum = OH.ShipmentNum
                            AND OH.StatusCode IN ( 0, 1 )
         ON OD.SkuNum = P.SkuNum
WHERE  P.SkuNum = 'XXXX'

You don't need a derived table here.

By placing the ON OD.SkuNum = P.SkuNum clause last the LEFT JOIN on Products logically happens last.

SELECT *
FROM   Products P
       LEFT OUTER JOIN OrderDetails OD
                       INNER JOIN OrderHeader OH
                         ON OD.ShipmentNum = OH.ShipmentNum
                            AND OH.StatusCode IN ( 0, 1 )
         ON OD.SkuNum = P.SkuNum
WHERE  P.SkuNum = 'XXXX'
梦萦几度 2024-12-10 11:28:08

不确定这是否是您要寻找的:

SELECT *
FROM OrderHeader OH
LEFT OUTER JOIN OrderDetails OD ON OD.ShipmentNum = OH.ShipmentNum AND OD.SkuNum = XXXX
WHERE OH.StatusCode IN (0, 1)

您的简单英语陈述似乎表明您正在寻找特定的货物。您可能需要将shipmentnum 添加到where 子句中。

Not sure if this is what you're looking for:

SELECT *
FROM OrderHeader OH
LEFT OUTER JOIN OrderDetails OD ON OD.ShipmentNum = OH.ShipmentNum AND OD.SkuNum = XXXX
WHERE OH.StatusCode IN (0, 1)

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.

浮光之海 2024-12-10 11:28:08

试试这个:

SELECT * 
FROM Products P
LEFT OUTER JOIN 
  ( OrderDetails OD 
     INNER JOIN OrderHeader OH ON (OD.ShipmentNum = OH.ShipmentNum
                               AND (OH.StatusCode = 0 OR OH.StatusCode = 1))
  ) ON OD.SkuNum = P.SkuNum
WHERE P.SkuNum = XXXX

Try this:

SELECT * 
FROM Products P
LEFT OUTER JOIN 
  ( OrderDetails OD 
     INNER JOIN OrderHeader OH ON (OD.ShipmentNum = OH.ShipmentNum
                               AND (OH.StatusCode = 0 OR OH.StatusCode = 1))
  ) ON OD.SkuNum = P.SkuNum
WHERE P.SkuNum = XXXX
執念 2024-12-10 11:28:08
SELECT  *
FROM    Products P
        LEFT JOIN (
            SELECT  OD.*
            FROM    OrderHeader OH
                    INNER JOIN OrderDetails OD
                        ON OH.ShipmentNum = OD.ShipmentNum
            WHERE   OH.StatusCode IN (0,1)
        ) as orders    
            ON P.SkuNum = orders.SkuNum
WHERE    P.SkuNum = XXXX            
SELECT  *
FROM    Products P
        LEFT JOIN (
            SELECT  OD.*
            FROM    OrderHeader OH
                    INNER JOIN OrderDetails OD
                        ON OH.ShipmentNum = OD.ShipmentNum
            WHERE   OH.StatusCode IN (0,1)
        ) as orders    
            ON P.SkuNum = orders.SkuNum
WHERE    P.SkuNum = XXXX            
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文