zoukankan      html  css  js  c++  java
  • 查询“全部”

    关系型数据库的查询语言(SQL)具有集合查询的特性,能够很方便地查询匹配任意一个数据的所有数据行,例如,有exists子句表示存在,表示匹配任意一行数据,但是如何表示匹配全部的数据行?例如,大学开设了C1-C9共计9门课程,如何查询选修了所有课程的学生?这就是“匹配全部”语义的一个经典实例。

    使用以下代码创建示例数据,后面我会使用两种不同的方式实现获取“选修全部”课程的学生的逻辑。

    --create table
    create table dbo.student
    (
    student_no int,
    student_name varchar(10)
    )
    
    create table dbo.course
    (
    course_no int,
    course_name varchar(10)
    )
    
    create table dbo.student_course
    (
    student_no int,
    course_no int
    )
    
    --insert data
    insert into dbo.student(student_no,student_name)
    values(1,'stu_a'),(2,'stu_b'),(3,'stu_c'),(4,'stu_d')
    
    insert into dbo.course(course_no,course_name)
    values(11,'English'),(12,'Math'),(13,'Chinese')
    
    insert into dbo.student_course(student_no,course_no)
    values(1,11),(1,12),(1,13),(2,11),(2,13),(3,11),(3,12),(4,12)
    View Code

    1,逆向思维,“双重否定”成肯定

    exists表示匹配任意一个,not exists表示不存在,使用两个 not exists 表示:不存在任何一个课程该学生没有选修,换句说法,就是该学生选修了所有的课程:

    select s.student_name
    from dbo.student s
    where not exists 
    (
        select NULL
        from dbo.course c
        where not EXISTS
        (
            select NULL
            from dbo.student_course sc
            where sc.student_no=s.student_no
                and sc.course_no=c.course_no
        )
    )

    2,正向思维,使用group by 子句实现

    根据学号对选课表进行分组,如果某一个学生选修的课程的数量和课程表的总数量相同,说明该学生选修了所有的课程:

    declare @CouseCount int
    
    select @CouseCount=count(0)
    from dbo.course
    
    ;with cte as 
    (
    select sc.student_no
    from dbo.student_course sc
    inner join dbo.course c
        on sc.course_no=c.course_no
    group by sc.student_no
    having count(0)=@CouseCount
    )
    select s.student_name
    from dbo.student s
    inner join cte c
        on s.student_no=c.student_no

    这是一种正向的思维方法,在思路上很好想明白,也比较好理解。

  • 相关阅读:
    REST接口设计规范总结
    Linux 下建立 Git 与 GitHub 的连接
    Linux centos修改防火墙为iptables
    一些开源软件的国内镜像源
    centOS7下安装GUI图形界面
    mysql使用锁的分析
    php后台添加默认图片
    php执行时间
    递归获取所有分类下面子分类的id
    php laravel左连接leftJoin多条where语句
  • 原文地址:https://www.cnblogs.com/ljhdo/p/4953322.html
Copyright © 2011-2022 走看看