2012-04-04 5 views
0

classid、optionid、 'images'のリスト(4つのテーブルから)を生成しようとしています。各製品には多数のClassID(1対多)があり、各ClassIDには多くのOptionIDがあり、各OptionIDには多数の画像があります。最後に、イメージテーブル(xcart_images_D)については、各productIDが持つイメージの数を数えたいと思います。複数のステートメントを使用せずにすべての情報を効果的に照会するにはどうすればよいですか?カウントで3つのテーブル結合の問題

ここでは、テーブルの各

xcart_products 
    + productid* 
    + product 
xcart_classes - 
    + classid* 
    + productid (xcart_products.productid) 
xcart_class_options - 
    + optionid* 
    + classid (xcart_classes.classid) 
    + option_name 
xcart_images_D - 
    + imageid* 
    + optionid (xcart_class_options.optionid) 
+ id (xcart_products.productid) 

* primary/foreign key 

のための基本的な情報があります私は3台が作業に参加していますが、私はxcart_images_Dテーブルを動作させることはできませんも私は数を達成する方法を理解してください。誰かが私を正しい方向に向けることができますか?それはここに価値がある何のため

は、私が試してみて、結果の数は約半分に減少している以下の行を追加すると私の3台が

SELECT xp.productid, xp.product, xc.classid, xco.optionid, xco.option_name 
FROM xcart_products xp 
JOIN xcart_classes xc ON xp.productid = xc.productid and xc.class = 'Color' 
JOIN xcart_class_options xco ON xc.classid = xco.classid 
WHERE xc.class = 'Color' 
ORDER by xp.productid DESC 

に参加しています。

JOIN xcart_images_D xi ON xi.optionid = xco.optionid 
GROUP BY xp.productid 

答えて

1
SELECT xp.productid, count(xi.imageid) 
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 
INNER JOIN xcart_images_D xi ON xi.optionid = xco.optionid 
GROUP BY xp.productid 
ORDER by xp.productid DESC 
+0

助けてくれてありがとう。 何らかの理由でカウントを追加すると、プルアップされた結果の数が2,000から約200に減少します。理由は何ですか? – Eric

関連する問題