gpt4 book ai didi

mysql - 如何在 Laravel Eloquent 方法或 SQL 中选择具有相同列条件的行

转载 作者:行者123 更新时间:2023-12-04 07:50:48 25 4
gpt4 key购买 nike

我有一个包含列的表“product_attribute_option”

product_id    attribute_id    attribute_option_id
1 9 15
1 15 23
2 9 15
2 11 18
2 12 19
3 9 15
3 15 23
现在我想通过 attribute_option 过滤产品。
如何获得具有attribute_option_id 15和23的不同product_ids?

最佳答案

您可以使用聚合和 having条款:

select product_id
from t
where attribute_option_id in (15, 23)
group by product_id
having count(distinct attribute_option_id) = 2;


关于mysql - 如何在 Laravel Eloquent 方法或 SQL 中选择具有相同列条件的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66990851/

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