gpt4 book ai didi

mysql - SQL 查询 - 对一个产品设置多个条件

转载 作者:太空宇宙 更新时间:2023-11-03 11:12:05 24 4
gpt4 key购买 nike

请帮我解决这个问题:我有一张像这样的 table :

id_feature  id_product  id_feature_value
1 1 50
2 1 54
5 1 67

我想像这样从这个表中选择:

table 中选择 count(id_product),其中 (id_feature = 1 AND id_feature_value = 50) AND (id_feature = 2 AND id_feature_value = 54) AND (id_feature = 5 AND id_feature_value = 67)

我的查询必须符合条件。比如有 count(condition) = 3

我不知道怎么写!

请帮帮我!对不起我的英语!

    SELECT count( pf.id_product ) AS nr_product, value, fv.id_feature_value, filter
FROM `nk_category_features` cat_f
INNER JOIN `nk_feature_value` fv ON fv.id_feature = '11'
AND fv.value IS NOT NULL
INNER JOIN `nk_product_features` pf ON pf.id_feature = '11'
AND pf.id_feature_value = fv.id_feature_value
INNER JOIN `nk_product` p ON p.id_product = pf.id_product
AND p.product_active = '1'
INNER JOIN `nk_product_features` pf1 ON ( pf1.id_feature = '14'
AND (
pf1.id_feature_value = '21'
) )
WHERE cat_f.id_feature = '11'
AND filter >0
GROUP BY pf.id_feature, pf.id_product
ORDER BY abs( fv.value ) ASC

这是我现在使用的查询,但我不喜欢宽度内连接的解决方案,同一张表上的内连接

最佳答案

试试这个:

select id_product,count(*)
from table
where (id_feature = 1 AND id_feature_value = 50)
OR (id_feature = 2 AND id_feature_value = 54)
OR (id_feature = 5 AND id_feature_value = 67)
group by id_product
having count(*) = 3

只得到满足where子句的产品总数

select count(*)
from
( select id_product
from table
where (id_feature = 1 AND id_feature_value = 50)
OR (id_feature = 2 AND id_feature_value = 54)
OR (id_feature = 5 AND id_feature_value = 67)
group by id_product
having count(*) = 3
) tmp

关于mysql - SQL 查询 - 对一个产品设置多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7820023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com