SQL 查询 - 在一个产品上放置多个条件

发布于 2024-12-10 17:49:48 字数 1215 浏览 0 评论 0原文

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

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

我想从这个表中进行选择,如下所示:

select count(id_product) from table where (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)

我的查询必须满足条件。就像 having 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

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

Please help me with this:
I have a table like:

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

And I want to select from this table like this:

select count(id_product) from table where (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)

my query must meet the conditions. Like having count(condition) = 3

I don't know how to write this!

Please help me! and sorry my english!

    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

this is my query that i used now, but i don't like the solution width inner join, inner join on the same table

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

じ违心 2024-12-17 17:49:48

试试这个:

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

try this:

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

To get just the total of products meeting the where clause

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文