2017-03-06 56 views
0

我需要組合,其可以或可以不具有它們中的數據2頁的表,但比我需要一個完整的外部聯接,其中最後表(如果有內容,其中IsActive = 1 )顯示的是數據,而不是合併的前兩個表。結合2分表用OUTER JOIN上的另一個表

目前有這樣的:

(SELECT qp.ItemName AS name 
      , qp.TimeAdded AS created 
      , '' AS effective 
      , qp.VendorName AS supplier 
      , qp.Source AS source 
      , qp.VendorType AS type 
      , qp.Price AS cost 
      , '' AS price 
      , '' AS markup 
      , '' AS customer 
      , '' AS customerListID 
      , qp.VendorListID AS vendorListID 
      , '' AS itemListID 
    FROM  wp_quantum_purchases AS qp 
    WHERE  qp.IsActive = 1 AND 
      NOT EXISTS (SELECT 1 
         FROM wp_hunter_quote_parts AS hqp 
         WHERE qp.ItemName = hqp.ItemName AND 
           hqp.IsActive = 1)) 
UNION ALL 
(SELECT qs.ItemName AS name 
      , qs.TimeAdded AS created 
      , qs.SalesDate AS effective 
      , '' AS supplier 
      , qs.Source AS source 
      , '' AS type 
      , '' AS cost 
      , qs.Price AS price 
      , '' AS markup 
      , qs.CustomerName AS customer 
      , qs.CustomerListID AS customerListID 
      , '' AS vendorListID 
      , '' AS itemListID 
    FROM  wp_quantum_sales AS qs 
    WHERE  qs.IsActive = 1 AND 
      NOT EXISTS (SELECT 1 
         FROM wp_hunter_quote_parts AS hqp 
         WHERE qs.ItemName = hqp.ItemName AND 
           hqp.IsActive = 1)) 
UNION ALL 
(SELECT hqp.ItemName AS name 
      , hq.Quote_Date AS created 
      , hqp.SalesDate AS effective 
      , hqp.VendorName AS supplier 
      , hqp.Source AS source 
      , hqp.VendorType AS type 
      , hqp.Cost AS cost 
      , hqp.Price AS price 
      , CAST((((CAST(hqp.Price AS DECIMAL(10, 2)) - CAST(hqp.Cost AS DECIMAL(10, 2)))/CAST(hqp.Cost AS DECIMAL(10, 2))) * 100) AS DECIMAL(10, 2)) AS markup 
      , IFNULL(hq.Customer_FullName, 'N/A') AS customer 
      , hq.Customer_ListID AS customerListID 
      , hqp.VendorListID AS vendorListID 
      , hqp.Item_ListID AS itemListID 
    FROM  wp_hunter_quote_parts AS hqp 
    LEFT JOIN wp_hunter_quotes AS hq 
    ON  (hq.id = hqp.QuoteID) 
    WHERE  hqp.IsActive = 1) 
ORDER BY NAME ASC; 

但是,這是在重複第1和第2個表中的數據並顯示數據的兩倍。我需要從第一和第二表中的數據將被合併爲1(如果存在),而是要在這裏爲內容,以從顯示最後表(wp_hunter_quote_parts)的優先級,如果存在最後一個表格(wp_hunter_quote_partsIsActive = 1。但是,如果IsActive = 1wp_hunter_quote_parts存在ItemName比我想都wp_quantum_purchaseswp_quantum_sales結合起來,好像它是1行。

不能做LEFT JOIN因爲數據可能在wp_quantum_purchases存在,但不是在wp_quantum_sales或數據可以在wp_quantum_sales存在,而不是在wp_quantum_purchases或數據不能在任一存在,只有在wp_hunter_quote_parts存在以及數據可能不存在於wp_hunter_quote_parts

所以,基本上,如果ITEMNAME存在wp_quantum_purchases和IsActive = 1 AND wp_hunter_quote_parts沒有ITEMNAME表,從wp_quantum_purchases獲得購買數據,否則,如果ITEMNAME存在wp_hunter_quote_partshunter_quote_parts獲得的數據來代替。

如果ITEMNAME在wp_quantum_sales存在和IsActive = 1 AND wp_hunter_quote_parts沒有ITEMNAME表,從wp_quantum_sales獲得銷售數據,否則,如果ITEMNAME存在wp_hunter_quote_partshunter_quote_parts獲得的數據來代替。

我怎麼能結合第一和​​第二臺,比作外就可以加入與另一個表?

另有企圖在這裏:

(SELECT IFNULL(qp.ItemName, qs.ItemName) AS name, IFNULL(qp.TimeAdded, qs.TimeAdded) AS created, qs.SalesDate AS effective, qp.VendorName AS supplier, qp.Source AS source, qp.VendorType AS type, qp.Price AS cost, qs.Price AS price, CAST((((CAST(qs.Price AS DECIMAL(10,2)) - CAST(qp.Price AS DECIMAL(10,2)))/CAST(qp.Price AS DECIMAL(10,2))) * 100) AS DECIMAL(10,2)) AS markup, qs.CustomerName AS customer, qs.CustomerListID AS customerListID, qp.VendorListID AS vendorListID, '' AS itemListID 
        FROM wp_quantum_purchases AS qp, wp_quantum_sales AS qs 
        WHERE (qp.IsActive = 1 OR qs.IsActive = 1) 
      AND NOT EXISTS (
       SELECT 1 
       FROM wp_hunter_quote_parts AS hqp 
       WHERE (qp.ItemName = hqp.ItemName || qs.ItemName = hqp.ItemName) AND hqp.IsActive = 1 
      ) 
       ) 
       UNION ALL 
      (SELECT hqp.ItemName AS name, hq.Quote_Date AS created, hqp.SalesDate AS effective, hqp.VendorName AS supplier, hqp.Source AS source, hqp.VendorType AS type, hqp.Cost AS cost, hqp.Price AS price, CAST((((CAST(hqp.Price AS DECIMAL(10,2)) - CAST(hqp.Cost AS DECIMAL(10,2)))/CAST(hqp.Cost AS DECIMAL(10,2))) * 100) AS DECIMAL(10,2)) AS markup, IFNULL(hq.Customer_FullName, 'N/A') AS customer, hq.Customer_ListID AS customerListID, hqp.VendorListID AS vendorListID, hqp.Item_ListID AS itemListID 
        FROM wp_hunter_quote_parts AS hqp 
        LEFT JOIN wp_hunter_quotes AS hq ON (hq.id = hqp.QuoteID) 
        WHERE (hqp.IsActive = 1)) 
      ORDER BY name ASC 

想通了這一個會的工作,但目前看來,它只是不斷去和去和去,似乎沒有不能完成的查詢。沒有錯誤,我可以看到,但沒有不能完成......而這些表是非常小的,那就是奇...

回答

0

我可能沒有完全理解你的問題,但你可以創建的視圖前兩個表,然後用第三個表進行外連接。

+0

不知道什麼創建視圖一樣。基本上,我需要將前2個表合併爲1,而不是在最後一個表上的外連接。如果最後一個表的IsActive = 1,其中ItemName被找到,我需要它返回表中的數據。合理? –