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

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

  • 相关阅读:
    UTF8转换为GB编码gb2312转换为utf-8
    localtime 和 localtime_r
    无损音频介绍
    bgr to rgb
    Farseer.Net
    为大家分享一个 Ajax Loading —— spin.js(转)
    HTML5-Ajax文件上传(转)
    Firefox火狐Flash插件卡死问题完美解决方法(转载)
    50 个最棒的 jQuery 日历插件,很齐全了!(转)
    linq to xml学习
  • 原文地址:https://www.cnblogs.com/ljhdo/p/4953322.html
Copyright © 2011-2022 走看看