Mysql - 具有相同标识符的多个值

时间:2014-09-29 14:23:44

标签: mysql prestashop-1.6

我正在努力解决问题。这是我的查询>

SELECT p.* 
FROM `ps_product` p 
LEFT JOIN `ps_product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.id_shop = 1 ) 
LEFT JOIN `ps_product_attribute` pa ON (pa.`id_product` = p.`id_product`) 
LEFT JOIN `ps_product_attribute_combination` pac ON (pac.`id_product_attribute` = pa.`id_product_attribute`) 

WHERE pl.`id_lang` = 2 
AND p.`id_category_default` = 25 
AND p.`active` = 1 
AND pac.`id_attribute` = 1714 
AND pac.`id_attribute` = 1736 
AND pac.`id_attribute` = 1737 
AND pac.`id_attribute` = 216 

ORDER BY p.`price` ASC

我的问题是只选择包含所有4个id_attributes的产品。

表ps_product_attribute_combination包含两列。 id_product_attribute和id_attribute。例如>

id_attribute  /  id_product_attribute
1714             120
1736             120
1737             120
216              120

我希望解释是成功的:) 谢谢!

2 个答案:

答案 0 :(得分:0)

我认为这应该有效:

SELECT p.* 
FROM `ps_product` p 
LEFT JOIN `ps_product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.id_shop = 1 ) 
LEFT JOIN `ps_product_attribute` pa ON (pa.`id_product` = p.`id_product`) 
LEFT JOIN `ps_product_attribute_combination` pac ON (pac.`id_product_attribute` = pa.`id_product_attribute`) 

WHERE pl.`id_lang` = 2 
AND p.`id_category_default` = 25 
AND p.`active` = 1 
AND 
    (
        pac.`id_attribute` = 1714 
        OR 
        pac.`id_attribute` = 1736 
        OR 
        pac.`id_attribute` = 1737 
        OR 
        pac.`id_attribute` = 216 
    )

ORDER BY p.`price` ASC

答案 1 :(得分:0)

您需要加入四次,每个属性对应您希望产品具有的属性:

SELECT p.* 
FROM `ps_product` p 
JOIN `ps_product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.id_shop = 1 ) 
JOIN `ps_product_attribute` pa ON (pa.`id_product` = p.`id_product`) 
JOIN `ps_product_attribute_combination` pac1 ON (pac1.`id_product_attribute` = pa.`id_product_attribute`) 
JOIN `ps_product_attribute_combination` pac2 ON (pac2.`id_product_attribute` = pa.`id_product_attribute`) 
JOIN `ps_product_attribute_combination` pac3 ON (pac3.`id_product_attribute` = pa.`id_product_attribute`) 
JOIN `ps_product_attribute_combination` pac4 ON (pac4.`id_product_attribute` = pa.`id_product_attribute`) 

WHERE pl.`id_lang` = 2 
AND p.`id_category_default` = 25 
AND p.`active` = 1 
AND pac1.`id_attribute` = 1714 
AND pac2.`id_attribute` = 1736 
AND pac3.`id_attribute` = 1737 
AND pac4.`id_attribute` = 216 

ORDER BY p.`price` ASC