gpt4 book ai didi

mysql - 检查单个列的多个值mysql

转载 作者:行者123 更新时间:2023-11-29 01:17:00 25 4
gpt4 key购买 nike

我有一个表 'optionsproducts' 具有以下结构和值

ID      OptionID      ProductID
1 1 1
1 2 1
1 3 1
1 2 2
1 2 3
1 3 3

现在我想提取同时分配了 OptionID 2 和 OptionID 3 的 ProductID。这意味着在这种情况下应该返回 ProductID 1 和 3。我不确定我错过了什么。任何帮助将不胜感激。

最佳答案

要获取与 2 和 3 OptionId 相关的 productID,您可以编写类似的查询:

select productid
from ( select productid
, optionid
, dense_rank() over(partition by productid
order by optionid) as rn
from t1
where optionid in (2,3)
) s
where s.rn = 2

结果:

PRODUCTID
----------
1
3

SQLFiddle Demo

关于mysql - 检查单个列的多个值mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18483535/

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