mysql - 选择 categoryId 属于 2 个或更多值的行

标签 mysql sql select

我有一个这样的表:

enter image description here

我想找到 courseId 属于 2 个或更多 categoryId 的 courseId。例如。如果我将 categoryId 指定为 18112 和 21115,那么它应该只提供 courseIds - 18602 和 21101。

我该怎么做?

最佳答案

courseId 分组并仅选择具有这 2 个 categoryId 的类(class)

select courseId
from your_table
where categoryId in (18112,21115)
group by courseId
having count(distinct categoryId) = 2

关于mysql - 选择 categoryId 属于 2 个或更多值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551052/

相关文章:

php - 如何更新具有空值的列

mysql - rails : Cannot connect to remote MySQL database

sql - 代理键作为复合键的外键

sql - MS Access SQL 记录在不应该消失的 LEFT JOIN 中消失

sql - 插入和返回新记录或现有记录的 ID,在 Postgres 12.5 中需要更好的策略

MySQL SELECT id of row where GREATEST of MAX entries of 几列

php - 如何获取Mysql中所有卷的总和?

如果子项不存在,Mysql 将行显示为空

sql - 了解 Postgres 查询计划

c# - 仅从 Entity Framework 中的表中检索第一行