1

私のクエリは、次の 5 つのテーブルを使用します。

categories c- categories_id and its other fields and parent_id field
categories_description cd-  categories_id and categories_name fields.
products_to_categories p2c- products_id linked to categories_id fields

products p- products_id field and other details
specials s- specials_status and products_id

私はこのMySQLクエリを持っています:

SELECT c.categories_id, cd.categories_name, c.parent_id 
FROM categories_description cd, categories c 
LEFT JOIN products_to_categories p2c on p2c.categories_id = c.categories_id 
LEFT JOIN products p ON p.products_id = p2c.products_id 
LEFT JOIN specials s ON p.products_id = s.products_id 
and s.specials_status = '1' 
WHERE c.categories_id = cd.categories_id and cd.language_id= 4 
and (c.parent_id = 0 OR c.parent_id = 1059 ) 
GROUP BY c.categories_id 
ORDER by sort_order, cd.categories_name

配列をフェッチした後、結果として次のようになります。

[1059] => Array
    (
        [1109] => Name of 1109
        [1323] => Name of 1323
        [1324] => Name of 1324
        [1322] => Name of 1322
        [1142] => Name of 1142
    )

[0] => Array
    (
        [1725] => Name of 1725
        [1077] => Name of 1077
        [95] => Name of 95
        [46] => Name of 46
        [96] => Name of 96
        [1059] => Name of 1059
        [1084] => Name of 1084
        [1386] => Name of 1386
        [1000] => Name of 1000
    )

アクティブなスペシャル (この場合は 1322) のないカテゴリを除外する方法がわかりません。LEFT JOIN を JOIN に置き換える:

SELECT c.categories_id, cd.categories_name, c.parent_id 
FROM categories_description cd, categories c 
JOIN products_to_categories p2c on p2c.categories_id = c.categories_id 
JOIN products p ON p.products_id = p2c.products_id 
JOIN specials s ON p.products_id = s.products_id 
and s.specials_status = '1' 
WHERE c.categories_id = cd.categories_id and cd.language_id= 4 
and (c.parent_id = 0 OR c.parent_id = 1059 ) 
GROUP BY c.categories_id 
ORDER by sort_order, cd.categories_name

サブカテゴリには正しい結果が得られますが、私のメインカテゴリは削除されます:

[1059] => Array
    (
        [1109] => Name of 1109
        [1323] => Name of 1323
        [1324] => Name of 1324
        [1142] => Name of 1142
    )

サブクエリを試しました:

SELECT c.categories_id, cd.categories_name, c.parent_id 
FROM categories_description cd, categories c 

JOIN ( select c.categories_id, cd.categories_name, c.parent_id FROM categories_description cd, categories c 

JOIN products_to_categories p2c on p2c.categories_id = c.categories_id 
JOIN products p ON p.products_id = p2c.products_id 
JOIN specials s ON p.products_id = s.products_id 
and s.specials_status = '1' 
WHERE c.categories_id = cd.categories_id  and cd.language_id= 4 and c.parent_id = 1059

 ) derived

WHERE c.categories_id = cd.categories_id and cd.language_id= 4 
and (c.parent_id = 0 OR c.parent_id = 1059 ) 
GROUP BY c.categories_id 
ORDER by sort_order, cd.categories_name

主なカテゴリのみを与える:

[0] => Array
    (
        [1725] => Name of 1725
        [1077] => Name of 1077
        [95] => Name of 95
        [46] => Name of 46
        [96] => Name of 96
        [1059] => Name of 1059
        [1084] => Name of 1084
        [1386] => Name of 1386
        [1000] => Name of 1000
    )
4

0 に答える 0