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

所以基本上每个产品都有一个类和一个变体,然后每个类都有类选项,每个变体都有变体项目。变体有自己的变体图像,每个类都有自己的“详细”图像(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
4

1 回答 1

1

在您提供表定义之前,这并不完全清楚。您是否尝试在第一个查询中将 count(xvi.variantid) 替换为 count(xiW.id) ?

更新:

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
于 2012-05-10T15:23:20.903 回答