2012-05-10 40 views
1

這已經涉及四個INNER JOIN和一個LEFT JOIN,但我不知道如何引用一個與LEFT JOIN表相似的表。可能引用具有一個查詢的兩個特定表

這裏是我的,因爲它是假設

SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiW.id, xiW.image_path, count(xvi.variantid) as cnt 
    FROM xcart_products xp 
    INNER JOIN xcart_variants xv ON xp.productid = xv.productid 
    INNER JOIN xcart_variant_items xvi ON xv.variantid = xvi.variantid 
    INNER JOIN xcart_class_options xco ON xvi.optionid = xco.optionid 
    INNER JOIN xcart_classes xc ON xco.classid = xc.classid AND xc.class = 'COLOR' 
    LEFT JOIN xcart_images_W xiW ON xiW.id = xvi.variantid 
    GROUP BY xco.optionid 
    ORDER by xp.product DESC 

所以基本上每一種產品同時具有class和一個變體,它的工作電流查詢,然後每個類都有類選項,以及各種變形具有商品款式。變體有自己的變體圖像,每個類都有自己的「詳細」圖像(xcart_images_D)。到目前爲止,我正在參考變體圖像,但我想要做的是不是計算xvi.variantid,而是要計算每個optionid有多少個詳細圖像。

我有一個查詢,這將做到這一點,但不幸的是我不能使用此查詢拉xcart_images_W。該查詢是belo

SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiD.image_path, xiD.path_on_server, count(xiD.optionid) as cnt 
    FROM xcart_products xp 
    INNER JOIN xcart_classes xc ON xp.productid = xc.productid AND xc.class = 'Color' 
    INNER JOIN xcart_class_options xco ON xc.classid = xco.classid 
    LEFT JOIN xcart_images_D xiD ON xiD.optionid = xco.optionid 
    GROUP BY xp.product, xco.optionid 
    ORDER by xp.product DESC 

是否有可能引用這兩個表或他們的外鍵使它不可能這樣做?

下面我提供了表結構。

+ xcart_products 
    - productid* 
    - product 
+ xcart_variants 
    - variantid* 
    - productid (xcart_products.productid) 
+ xcart_variant_items [bridge table] 
    - optionid* 
    - variantid (xcart_variants.variantid) 
+ xcart_classes 
    - classid* 
    - productid (xcart_products.productid) 
    - class 
+ xcart_class_options 
    - optionid* 
    - option_name 
    - classid (xcart_classes.classid) 
+ xcart_images_W 
    - imageid* 
    - id (xcart_variants.variantid) 
    - image_path 
+ xcart_images_D 
    - imageid* [not relational with xcart_images_W.imageid] 
    - id (xcart_products.productid) 
    - optionid (xcart_class_options.optionid) 
    - image_path 

* Primary Key 
() relational data 
[] notes 

目前工作但雜亂的查詢。我如何清理?

SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiW.id, xiW.image_path 
FROM xcart_products xp 
INNER JOIN xcart_variants xv ON xp.productid = xv.productid 
INNER JOIN xcart_variant_items xvi ON xv.variantid = xvi.variantid 
INNER JOIN xcart_class_options xco ON xvi.optionid = xco.optionid 
INNER JOIN xcart_classes xc ON xco.classid = xc.classid AND xc.class = 'COLOR' 
LEFT JOIN xcart_images_W xiW ON xiW.id = xvi.variantid 
LEFT JOIN (
SELECT COUNT(xiD.optionid) as dCount 
    FROM xcart_products xp2 
    INNER JOIN xcart_classes xc2 ON xp2.productid = xc2.productid AND xc2.class = 'Color' 
    INNER JOIN xcart_class_options xco2 ON xc2.classid = xco2.classid 
    LEFT JOIN xcart_images_D xiD ON xiD.optionid = xco2.optionid 
) ON xiW.id = xvi.variantid 
GROUP BY xco.optionid 
ORDER by xp.product DESC 

回答

1

直到您提供表定義之後纔會完全清楚。你有沒有嘗試在第一個查詢中用count(xiW.id)替換count(xvi.variantid)?

更新:

SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiW.id, xiW.image_path, count(xvi.variantid) as cnt 
FROM xcart_products xp 
INNER JOIN xcart_variants xv ON xp.productid = xv.productid 
INNER JOIN xcart_variant_items xvi ON xv.variantid = xvi.variantid 
INNER JOIN xcart_class_options xco ON xvi.optionid = xco.optionid 
INNER JOIN xcart_classes xc ON xco.classid = xc.classid AND xc.class = 'COLOR' 
LEFT JOIN (
    SELECT COUNT(xiD.id) as Dcount 
    FROM xcart_images_D xiD 
    INNER JOIN xcart_images_W xiW ON xiW.column = xiD.column /*please update accordingly */ 
) ON xiW.id = xvi.variantid 
GROUP BY xco.optionid 
ORDER by xp.product DESC 

再次更新:

SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name, xiW.id, xiW.image_path, count(xvi.variantid) as cnt, xiD.totalD 
FROM xcart_products xp 
INNER JOIN xcart_variants xv ON xp.productid = xv.productid 
INNER JOIN xcart_variant_items xvi ON xv.variantid = xvi.variantid 
INNER JOIN xcart_class_options xco ON xvi.optionid = xco.optionid 
INNER JOIN xcart_classes xc ON xco.classid = xc.classid AND xc.class = 'COLOR' 
LEFT JOIN (
    SELECT count(xiD.id) as totalD, xiD.optionid FROM xcart_images_D xiD WHERE xiD.optionid = xvi.optionid 
) ON xiD.optionid = xvi.optionid 
GROUP BY xco.optionid 
ORDER by xp.product DESC 
+0

嗨JA謝謝你的幫助。我將提供表格的佈局,但我早先嚐試了您的建議,問題是每個xiW都有自己的一組圖像,xiD也是如此。當我計算xiW時,它計算變體圖像的數量,我正在尋找的是詳細圖像而不是變體圖像的計數。 – Eric

+1

您能否提供xiW和xiD表格定義。此外,當你執行上面的查詢時會發生什麼(更新我的文章以提高可讀性)? –

+0

當然JA,我繼續並在原始文章中添加了引用表的重要信息。 – Eric

相關問題