-- 学生表
CREATE TABLE STUDENT
(
S_ID INT PRIMARY KEY NOT NULL,
S_NAME VARCHAR2(80),
S_BIRTH VARCHAR(20),
S_SEX VARCHAR2(10)
)
-- 课程表
CREATE TABLE COURSE
(
C_ID INT PRIMARY KEY NOT NULL,
C_NAME VARCHAR2(80),
T_ID INT
)
-- 教师表
CREATE TABLE TEACHER
(
T_ID INT PRIMARY KEY NOT NULL,
T_NAME VARCHAR2(80)
)
-- 成绩表
CREATE TABLE SCORE
(
S_ID INT,
C_ID INT,
S_SCORE INT
);
SELECT * FROM STUDENT;
SELECT * FROM COURSE;
SELECT * FROM TEACHER;
SELECT * FROM SCORE ;
DROP TABLE STUDENT;
DROP TABLE COURSE;
DROP TABLE TEACHER;
DROP TABLE SCORE;
-- 插 入 学 生 表 测 试 数 据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
-- 课 程 表 测 试 数 据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
insert into Course values('04' , '政治' , '04');
-- 教 师 表 测 试 数 据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
insert into Teacher values('04' , '赵六');
-- 成 绩 表 测 试 数 据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('05' , '04' , 76);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('06' , '04' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
insert into Score values('07' , '04' , 100);
查询和 01 同学学习的课程完全相同的同学的信息
SELECT *
FROM STUDENT
WHERE S_ID IN
(SELECT S_ID
FROM SCORE
WHERE C_ID NOT IN
(SELECT C_ID
FROM COURSE
WHERE C_ID NOT IN (SELECT C_ID FROM SCORE WHERE S_ID = 01))
GROUP BY S_ID
HAVING COUNT(C_ID) = (SELECT COUNT(1) FROM SCORE WHERE S_ID = 01) AND S_ID <> 01);
SELECT * FROM SCORE;
SELECT C.*
FROM STUDENT C
JOIN (SELECT A.S_ID
FROM SCORE A
JOIN SCORE B
ON A.S_ID = B.S_ID
AND A.C_ID = B.C_ID
AND A.C_ID IN (SELECT C_ID FROM SCORE WHERE S_ID = 01)
GROUP BY A.S_ID
HAVING COUNT(A.C_ID) = (SELECT COUNT(C_ID) FROM SCORE WHERE S_ID = 01)) D
ON C.S_ID = D.S_ID
AND D.S_ID <> 01;