关系型数据库的查询语言(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

相关文章:

  • 2021-10-13
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
  • 2022-02-25
  • 2022-01-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-13
  • 2022-01-09
  • 2022-02-17
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案