简体   繁体   中英

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.

You do not need to have AND pq.product_id = products.id in the where statement. Because you are LEFT JOIN ing on that. 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. 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. 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. Instead, you must do that filter in the On clause outside the dervied table definition.

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. If that is the case, then simply expand the derived table to include the filters on the outer products table.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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