这已经涉及四个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
答案 0 :(得分: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