简体   繁体   English

MySQL查询购物车过滤器

[英]MySQL query for shopping cart filter

I am using the below table for shopping cart 我正在使用下表购物车

id  product_id  attribute_id    value
----------------------------------------
1       1           1           A,B,C
2       2           1           B,C
3       3           1           C
4       1           2           200
5       2           2           150
6       3           2           300
7       1           3           RED
8       2           3           BLUE
9       3           3           RED,GREEN
10      1           4           YES
11      2           4           NO
12      3           4           NO

I am able to form a search result from this table. 我可以从此表中形成搜索结果。 There is a provision to filter the search result by attributes. 提供了按属性过滤搜索结果的条款。

Attributes can hold the following types of values: 属性可以包含以下类型的值:

  1. Numeric (250) 数值(250)
  2. String (YES/NO) 字符串(是/否)
  3. String List (A,B,C) 字符串列表(A,B,C)

I need a query to get list of product_id for the below conditions 我需要查询以获取以下条件的product_id列表

  • attribute_id = 1 and value = B or C attribute_id = 1且值= B或C
  • and
  • attribute_id = 2 and value = 150 attribute_id = 2并且值= 150
  • and
  • attribute_id = 4 and value = NO attribute_id = 4,值= NO

I referred a stock question ( MySQL Multiple Where Clause ) and tried but not able to get the actual output. 我提到了一个库存问题( MySQL Multiple Where Clause )并尝试了但无法获得实际输出。

Use OR in your WHERE clause and also use brackets to split each condition. WHERE子句中使用OR ,并使用方括号拆分每个条件。

SELECT id, product_id, attribute_id, value
FROM shoppingcart
WHERE (attribute_id = 1 AND value IN ('B', 'C'))
OR (attribute_id = 2 AND value = 150)
OR (attribute_id = 4 AND value = 'NO')

Try this: 尝试这个:

SELECT product_id FROM table_name WHERE 

(attribute_id = 1 AND (
            (value LIKE 'B,%' OR value LIKE '%,B' OR value LIKE '%,B,%' OR value LIKE 'B')
            OR
            (value LIKE 'C,%' OR value LIKE '%,C' OR value LIKE '%,C,%' OR value LIKE 'C')
              )
) 

OR

(attribute_id = 2 AND value = '150')
OR

(attribute_id = 3 AND value = 'NO')

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

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