MySQL查询没有给出正确的结果

时间:2013-10-16 14:58:05

标签: mysql

我的查询使用了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
    )

0 个答案:

没有答案
相关问题