2012-05-02 107 views
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' 

這是不允許第一個「產品」表中左外連接查詢。

回答

1

問題是是派生表不相關子查詢。因此,您不能從派生表的定義中引用外部表。在這種情況下,您無法參照定義中的外部products表。相反,您必須在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 

現在,你已經在註釋中說明這是太慢了,因爲,說哪裏刪除<> 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 
+0

它會評估customfields_oo查詢只有一次或外部主要選擇查詢的每一行? –

+0

@DhavalDarji - 它將評估'customfields_oo'查詢一次,然後將該結果集連接到外部查詢。因此,內部查詢執行一次,合併/連接/散列(取決於優化引擎)結果到外部表(「產品」)。 – Thomas

+0

謝謝。查詢執行罰款上述計算,但如果我想從報價表計算那些具有當前產品的最後一個訂單日期,我必須傳遞customfield_oo查詢中的當前產品ID其他它正在計算錯誤的結果(即它是基於所有產品)。 –

1

你不需要在where語句中有AND pq.product_id = products.id。因爲你是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 
..... 
+0

如果我刪除「並pq.product_id = products.id」,那麼該查詢將工作,但那麼它會計算公式爲每個產品行,然後挑選匹配「ON customfields_oo.product_id =產品1排.id「當前產品,但認爲我爲5000個產品執行此操作時,它會計算每個5000次迭代的每個產品行!謝謝,因爲你回覆我試過這個,但是對於其他的計算,它給出錯誤的結果,所以我必須把這個條件和交替的方式。 –

+0

更新了你的答案 – Arion

+0

是的,這將做任務,但我不能使用這種語法,因爲它會創建其他計數查詢問題,因爲我在sugarcrm上工作有限制主查詢中的子查詢。 :) –