這是我的第一個問題。我搜索了很多文檔,但找不到答案。
我有3張桌子。在MySQL中使用多個OR/AND進行篩選
product_main(with idx and name)<br />
proudct_attributes (with idx 'pid gid value)<br />
product_attributes_groups (with idx name) <br />
現在我試圖按屬性過濾產品。我有一個單獨的屬性沒有問題,但我不能讓它工作與幾個條件,即(紅色或藍色的,但只有新)過濾
SELECT `pid` FROM `product_attributes`
LEFT JOIN `product_main` ON `product_attributes`.`pid` = `product_main`.`idx`
LEFT JOIN `product_attributes_groups` ON `product_attributes`.`gid` = `product_attributes_groups`.`idx`
WHERE
(
(`product_attributes_groups`.`name` = 'color' AND `product_attributes`.`value` ='red')
OR
(`product_attributes_groups`.`name` = 'color' AND `product_attributes`.`value` ='blue')
)
AND
(`product_attributes_groups`.`name` = 'condition' AND `product_attributes`.`value` ='new')
編輯:
這幫助: 選擇pm
。 idx
from product_attributes
as pa
join product_main
as pm
on pa
。 pid
= pm
。 idx
加入 product_attributes_groups
as pig
on pa
。 gid
= pig
。 idx
組由pm
組成。 idx
具有 總和(pig
。name
= '顏色' AND pa
。value
在( '紅', '藍'))> 0和 總和(pig
。name
= '狀態' AND pa
。value
= '使用')> 0;
通過Gordon,稍作修改。
而你的問題是? – hakre
這是EAV嗎?一般來說,我避免像瘟疫一樣的EAV。在我不得不使用它的地方,我更喜歡構建一個僞歸一化的「物化視圖」,然後將所需的條件附加到該視圖上。 – Strawberry