![](/img/trans.png)
[英]Product Name not returning in custom grid on filter search in Magento
[英]search product with their specifications filter
我有一個包含其規格產品的表。我在http://sqlfiddle.com/#!2/15575/1中創建了一個數據樣本。我想要創建搜索表單,以便用戶可以按其規格搜索產品。用戶搜索具有RAM = 2和VGA = 512或VGA = 1的筆記本電腦。 我寫了一個查詢,但是沒有用。請幫助我。 這個例子:結果:1525,1535,k5,k6
select product
from my_table
where (custom = 'Ram' and custom_value = '2')
or (custom = 'vga' and custom_value in ('1', '512'))
group by product
having count(distinct custom) = 2
例:
我將您的請求解釋為:-在每個條件中,您都需要一個OR
子句。 -所有標准都進行“ AND
運算。
因此,對於上面的示例,您需要:
SELECT products.product FROM
(select distinct product from my_table) as products
where
exists (
select * from my_table where products.product = my_table.product
and custom='Ram' and custom_value='2')
and exists (
select * from my_table where products.product = my_table.product
and custom='vga' and custom_value in('512','1'))
從產品的唯一列表開始,然后檢查每個條件以查看該產品是否存在值。
SQL小提琴在這里: http ://sqlfiddle.com/#!2/15575/35
話雖如此,您應該重新設計表格。
您可以從該查詢中看到重新設計的起點。 首先,創建一個僅列出獨特產品的表。 然后為每個條件創建表。 或展開獨特產品表,以將每個自定義值(“ Ram”,“ vga”等)作為獨特產品表中的列。
您想要一個or
擁有一個的地方and
我認為:
SELECT product, count(*) as prod_count FROM my_table
WHERE (custom='Ram' and custom_value='2')
or (custom='vga' and custom_value in('512','1'))
group by product
having prod_count = 2
首先,我建議您使用其他數據庫結構:
CREATE TABLE COMPANY (ID INT, NAME VARCHAR(50), PRIMARY KEY(ID));
CREATE TABLE RAM (ID INT, NAME VARCHAR(50), PRIMARY KEY(ID));
CREATE TABLE CPU (ID INT, NAME VARCHAR(50), PRIMARY KEY(ID));
CREATE TABLE VGA (ID INT, NAME VARCHAR(50), PRIMARY KEY(ID));
然后使用關系表指定產品屬性:
CREATE TABLE PRODUCT_PROPERTIES (
ID_PRODUCT INT REFERENCES PRODUCT(ID),
ID_COMPANY INT REFERENCES COMPANY (ID),
ID_RAM INT REFERENCES RAM (ID),
ID_CPU INT REFERENCES CPU (ID),
ID_VGA INT REFERENCES VGA (ID),
PRIMARY KEY(ID_PRODUCT)
);
然后,在PRODUCT_PROPERTIES
上使用簡單的JOINS和簡單的SELECT,就可以輕松過濾結果。
希望這可以幫助..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.