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

标签 mysql sql laravel eloquent

我有一个包含列的表“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_id?

最佳答案

您可以使用聚合和 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/

相关文章:

php - 如何在 "or"部分编写自定义 "where"条件; zf2 Zend框架

php - 向大型 MYSQL 数据库添加新字段

mysql "AND"日期时间列上的子句两次

sql - 在 PostgreSQL 中跨多个表索引列

mysql - 在列中插入多个值

sql - Excel导入Access

php - SQL - SELECT 计算多个字段

php - 动态调查应用程序数据库结构

php - 拉维尔 5 : allow user to edit post if he's the owner of the post or the owner of the forum category

php - 我的 Laravel 5.2.10 session 不会持续