mysql - 如何以更好的方式重写这个嵌套查询?

标签 mysql sql oracle

我有这个 sql,它可以找到参加过 ID 为 10101 的讲师教授的类(class)部分的(不同的)学生总数。

            select count (distinct ID)
            from takes
            where (course_id, sec_id, semester, year) in 
            (select course_id, sec_id, semester, year
            from teaches
            where teaches.ID= 10101);

重写它的另一种或最佳方法是什么。

我们将感谢您的帮助。

最佳答案

为什么不用 ANSI Join?

select 
    count (distinct t1.ID) 
from 
    takes as t1
    inner join teaches as t2 on 
        t1.course_id=t2.course_id and
    t1.sec_id=t2.sec_id and
    t1.semester=t2.semester and
    t1.year=t2.year 
where 
    t2.ID= 10101

关于mysql - 如何以更好的方式重写这个嵌套查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11576279/

相关文章:

php - 无法为 INT 类型保存空值(php、symfony)

mysql - SQL 如果子字符串出现则选择然后复制直到子字符串否则保持原始

oracle - Jpa - Hibernate ManyToMany 在连接表中进行多次插入

sql - 哪些代码在数据库中有对应关系

sql - 在 Oracle 中创建数据库链接时出错

mysql - 统一并简化 MySQL 命令

mysql - 错误 1292 - 截断了不正确的 DOUBLE 值 :'70,40,77,28,23,72,82,38,29'

php - 如何构建一个查询来计算所有在我之后注册的用户?

mysql - 减少 MySQL 中的 SELECT 查询时间

MYSQL - 插入两个值,其中另一个表的条件不起作用