[英]Searching products by specific attributes
I've a little Shop-system and now I want to select some product with specific attributes and conditions. 我有一个小商店系统,现在我想选择一些具有特定属性和条件的产品。
Here my Setup: 这是我的设置:
table XYZ: XYZ表:
So now I want to select all products which has the attribute-ids 1,3,5 and 7 but not 9 and 2. 所以现在我要选择所有具有属性ID 1、3、5和7而不是9和2的产品。
How can I do this on a simple way, my first idea was to group it by product id and group_concat the attribute_ids and use "having" to include and exclude the attribute ids I want. 我如何以一种简单的方式执行此操作,我的第一个想法是按产品ID对其进行分组,并对attribute_ids进行分组,并使用“具有”来包含和排除我想要的属性ID。
SELECT CONCAT('_',GROUP_CONCAT(attribute_id SEPARATOR '_'),'_') as attrs,
product_id
FROM my_table
GROUP BY product_id
HAVING (attrs LIKE '%_1_%'
AND attrs LIKE '%_3_%'
AND attrs LIKE '%_5_%'
AND attrs LIKE '%_7_%')
AND (attrs NOT LIKE '%_2_%'
AND attrs NOT LIKE '%_9_%')
This works, but there is a better solution, right? 这可行,但是有更好的解决方案,对吧?
Maybe this would help? 也许这会有所帮助?
SELECT product_id FROM table_XYZ WHERE attribute_id IN (1,3,5,7); 从table_XYZ的WHERE attribute_id IN(1,3,5,7)中选择product_id
Or if you have more ids and what to exclude only a few: 或者,如果您有更多ID,并且仅排除少数几个:
SELECT product_id FROM table_XYZ WHERE attribute_id NOT IN (2,9); 从table_XYZ的SELECT product_id中选择attribute_id NOT IN(2,9);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.