简体   繁体   English

Mysql:对多条记录应用条件

[英]Mysql: applying conditions on multiple records

I have a MySQL table to store product variants option values (eg: colour, size, weight, the height of product variants).我有一个 MySQL 表来存储产品变体选项值(例如:颜色、尺寸、重量、产品变体的高度)。 It contains the following data.它包含以下数据。

在此处输入图片说明

I have a filter option on the frontend, where users can select filter options such as colour, size, weight, height etc and I need to show product variants based on the filter values.我在前端有一个过滤器选项,用户可以在其中选择过滤器选项,例如颜色、尺寸、重量、高度等,我需要根据过滤器值显示产品变体。

In the above example: option_id = 110 means size.在上面的例子中:option_id = 110 表示大小。 option_id = 109 means color. option_id = 109 表示颜色。 Suppose a user-selected size = 31 (XL) and colour = 27 (Red),假设用户选择的尺码 = 31 (XL) 和颜色 = 27 (红色),

I have to find a product_variant_id where option_id = '110' AND option_value_id = 31 AND option_id = '109' AND option_value_id = 27. (result should be records with ids 266 and 267)我必须找到一个 product_variant_id 其中 option_id = '110' AND option_value_id = 31 AND option_id = '109' AND option_value_id = 27.(结果应该是 ids 266 和 267 的记录)

There is only one such product_variant_id that is 140.只有一个这样的 product_variant_id 是 140。

I tried我试过

SELECT * FROM product_variant_option_values WHERE (option_id = '110' AND option_value_id = 31) OR (option_id = '109' AND option_value_id = 27) SELECT * FROM product_variant_option_values WHERE (option_id = '110' AND option_value_id = 31) OR (option_id = '109' AND option_value_id = 27)

but it's not returning what I want, the above query returns但它没有返回我想要的,上面的查询返回

在此处输入图片说明

OK, after seeing the result you are getting right now.好的,在看到你现在得到的结果后。 If I understand correctly, you want product_variant_id that has exactly the two conditions you mentioned.如果我理解正确,您希望product_variant_id恰好具有您提到的两个条件。 So one option for a query could be:因此,查询的一种选择可能是:

SELECT product_variant_id
FROM product_variant_option_values 
WHERE (option_id = '110' AND option_value_id = 31) OR (option_id = '109' AND option_value_id = 27)
GROUP BY product_variant_id
HAVING count(*) = 2

Which means, that you count the number of rows returned per each product_variant_id and only return the one that has 2 rows.这意味着,您计算每个product_variant_id返回的行数,并且只返回具有 2 行的行数。 Which means got the two conditions.这意味着得到了两个条件。

I think what you can do is to add a COUNT() operation to see if both of the filter condition are met.我认为您可以做的是添加一个COUNT()操作来查看是否满足两个过滤条件。 Something like below:像下面这样:

SELECT   product_variant_id,COUNT(*) cnt 
FROM     product_variant_option_values 
WHERE    (option_id = '110' AND option_value_id = 31) 
  OR     (option_id = '109' AND option_value_id = 27)
GROUP BY product_variant_id
HAVING   cnt >= 2;

Grouped by product_variant_id whereby the result of COUNT() is 2 or more;product_variant_id分组,其中COUNT()的结果为 2 或更多; of if the filter condition is always going to be 2 then just HAVING cnt=2 should suffice.如果过滤条件始终为 2,那么只需HAVING cnt=2就足够了。 This query can similarly be written this way:这个查询可以类似地写成这样:

SELECT   product_variant_id,COUNT(*) cnt 
FROM     product_variant_option_values 
WHERE    option_id in ('110','109') 
  AND    option_value_id IN (27,31)
GROUP BY product_variant_id
HAVING   cnt >= 2;

Fiddle example : https://www.db-fiddle.com/f/9kKmyHsSAMNk4BBPDWLHFh/3小提琴示例: https : //www.db-fiddle.com/f/9kKmyHsSAMNk4BBPDWLHFh/3

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

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