0

在下面的查询中,我尝试使用左外连接中的第一个表。但是我收到一个错误。

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

当我运行查询时,它给了我以下错误:

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

它不允许左外连接查询中的第一个“产品”表。

4

2 回答 2

1

问题是customfields_oo派生表不是相关子查询。因此,您不能从派生表的定义中引用外部表。在这种情况下,您不能productscustomfields_oo定义中引用外部表。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

现在,您在评论中说这太慢了,因为删除 <> 0 的产品可能会在派生表中进行评估。如果是这种情况,则只需扩展派生表以包含外部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
于 2012-05-03T02:19:14.247 回答
1

你不需要AND pq.product_id = products.id在where语句中有。因为你正在LEFT JOIN考虑这一点。所以我认为这样的事情会起作用:

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

编辑

你不需要LEFT JOIN在你正在工作的桌子COUNT上。你也可以这样做:

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
 .....
于 2012-05-02T08:36:35.660 回答