简体   繁体   English

搜索产品及其规格过滤器

[英]search product with their specifications filter

I have a table that contain products with their specifications.i created a sample of data in http://sqlfiddle.com/#!2/15575/1 .I want create search form that users can search products by their specifications.for example user search laptop with RAM=2 and VGA=512 or VGA=1. 我有一个包含其规格产品的表。我在http://sqlfiddle.com/#!2/15575/1中创建了一个数据样本。我想要创建搜索表单,以便用户可以按其规格搜索产品。用户搜索具有RAM = 2和VGA = 512或VGA = 1的笔记本电脑。 I wrote a query,but not working.please help me. 我写了一个查询,但是没有用。请帮助我。 this example: result: 1525,1535,k5,k6 这个例子:结果: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

Example: 例:

http://sqlfiddle.com/#!2/15575/19 http://sqlfiddle.com/#!2/15575/19

I'm interpreting your request as: - Within each of your criteria, you want an OR clause. 我将您的请求解释为:-在每个条件中,您都需要一个OR子句。 - all the criteria are AND ed together. -所有标准都进行“ AND运算。

So, for your sample above, you would want: 因此,对于上面的示例,您需要:

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'))

Where you start with the unique list of products, and then check each criteria to see if a value exists for that product. 从产品的唯一列表开始,然后检查每个条件以查看该产品是否存在值。

SQL Fiddle here: http://sqlfiddle.com/#!2/15575/35 SQL小提琴在这里: http ://sqlfiddle.com/#!2/15575/35

Having said that, you should redesign your tables. 话虽如此,您应该重新设计表格。

You can see from this query what a starting point to a redesign might be. 您可以从该查询中看到重新设计的起点。 First, create a table that just lists the unique products. 首先,创建一个仅列出独特产品的表。 Then create tables for each of the criteria. 然后为每个条件创建表。 Or expand the table of the unique products to have each of those custom values ('Ram', 'vga', etc.) as columns in the table of unique products. 或展开独特产品表,以将每个自定义值(“ Ram”,“ vga”等)作为独特产品表中的列。

You want an or where you have an and I think: 您想要一个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

First of all, I'd recommand you use a different DB structure: 首先,我建议您使用其他数据库结构:

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));

Then use a relational table to specify products properties: 然后使用关系表指定产品属性:

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)
);

Then using simple JOINS and a simple SELECT on your PRODUCT_PROPERTIES you can filter your results easily. 然后,在PRODUCT_PROPERTIES上使用简单的JOINS和简单的SELECT,就可以轻松过滤结果。

Hope this helps.. 希望这可以帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM