简体   繁体   English

如何在LEFT OUTER JOIN中访问外部表

[英]How to access outer table in a LEFT OUTER JOIN

In the query below, I am trying to use the first table in a left outer join. 在下面的查询中,我试图在左外连接中使用第一个表。 However I am getting an error. 但是我收到了一个错误。

SELECT
       products.id,
       products_cstm.oem_c,
       products.mfr_part_num,
       products.description,
       products.cost,
       products.assigned_user_id,
       customfields_oo.ans
FROM products
          LEFT OUTER JOIN (SELECT  COUNT( q.id )  AS ans
                                  , pq.product_id
                           FROM   products_quotes pq 
                                     LEFT JOIN quotes q
                                           ON pq.quote_id = q.id
                           WHERE  q.deleted = 0
                           AND    pq.deleted = 0
                           AND    q.stage <> 4
                           AND  (pq.qty_shipped < pq.product_qty)
                           AND  pq.product_id = products.id
                           GROUP BY pq.product_id
                      ) AS customfields_oo
                ON customfields_oo.product_id = products.id
          LEFT JOIN products_cstm
                ON products.id = products_cstm.id_c
WHERE  products.deleted = 0
ORDER  BY ans DESC

When I run the query it gives me the following error: 当我运行查询时,它给我以下错误:

Error Code : 1054
Unknown column 'products.id' in 'where clause'

It is not allowing first "products" table in left outer join query. 它不允许在左外连接查询中使用第一个“products”表。

You do not need to have AND pq.product_id = products.id in the where statement. 您不需要在where语句中使用AND pq.product_id = products.id Because you are LEFT JOIN ing on that. 因为你是LEFT JOIN ing。 So I think something like this will work: 所以我认为这样的事情会起作用:

AND (pq.qty_shipped < pq.product_qty)
GROUP BY pq.product_id) AS customfields_oo
ON customfields_oo.product_id = products.id
LEFT JOIN products_cstm
   ON products.id = products_cstm.id_c
WHERE products.deleted = 0
ORDER BY openorder DESC

EDIT 编辑

You do not need to LEFT JOIN on the table you are COUNT ing on. 你不需要LEFT JOIN上你的表COUNT荷兰国际集团上。 You can also do ot like this: 你也可以这样做:

SELECT
    .....
    (
        SELECT
           COUNT( q.id )
        FROM products_quotes pq
        LEFT JOIN quotes q
           ON pq.quote_id = q.id
        WHERE q.deleted = 0
        AND pq.deleted = 0
        AND q.stage <> 4
        AND (pq.qty_shipped < pq.product_qty)
        AND pq.product_id = products.id
    ) AS ans
 FROM products
 .....

The issue is that customfields_oo is a derived table not a correlated subquery. 问题是customfields_oo是派生表而不是相关子查询。 Thus, you cannot reference the outer table from within the definition of the derived table. 因此,您无法从派生表的定义中引用外部表。 In this case, you cannot refer to the outer products table from within the customfields_oo definition. 在这种情况下,您无法从customfields_oo定义中引用外部products表。 Instead, you must do that filter in the On clause outside the dervied table definition. 相反,您必须在dervied表定义之外的On子句中执行该过滤。

Select products.id,
       products_cstm.oem_c,
       products.mfr_part_num,
       products.description,
       products.cost,
       products.assigned_user_id,
       customfields_oo.ans
FROM products
    Left Join   (
                Select pq1.product_id
                    , Count( q1.id ) As ans
                From products_quotes As pq1 
                    Left Join quotes As q1
                        On pq1.quote_id = q1.id
                Where q1.deleted = 0
                    And pq1.deleted = 0
                    And q1.stage <> 4
                    And pq1.qty_shipped < pq1.product_qty
                Group By pq1.product_id
                ) As customfields_oo
        On customfields_oo.product_id = products.id
    Left Join products_cstm
        On products.id = products_cstm.id_c
Where products.deleted = 0
Order By customfields_oo.ans Desc

Now, you have stated in comments that this is too slow because, say products where deleted <> 0 might be evaluated in the derived table. 现在,您已经在评论中声明这太慢了,因为在派生表中可能会评估已删除<> 0的产品。 If that is the case, then simply expand the derived table to include the filters on the outer products table. 如果是这种情况,则只需展开派生表以在外部products表上包含过滤器。

Select products.id,
       products_cstm.oem_c,
       products.mfr_part_num,
       products.description,
       products.cost,
       products.assigned_user_id,
       customfields_oo.ans
FROM products
    Left Join   (
                Select pq1.product_id
                    , Count( q1.id ) As ans
                From products_quotes As pq1 
                    Join products As p1
                        On p1.products.id = pq1.product_id
                    Left Join quotes As q1
                        On pq1.quote_id = q1.id
                Where q1.deleted = 0
                    And pq1.deleted = 0
                    And q1.stage <> 4
                    And pq1.qty_shipped < pq1.product_qty
                    And p1.deleted = 0
                Group By pq1.product_id
                ) As customfields_oo
        On customfields_oo.product_id = products.id
    Left Join products_cstm
        On products.id = products_cstm.id_c
Where products.deleted = 0
Order By customfields_oo.ans Desc

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

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