1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号 –3.教师表 Teacher(t_id,t_name) –教师编号,教师姓名 –4.成绩表 Score(s_id,c_id,s_score) –学生编号,课程编号,分数
CREATE TABLE student(
s_id VARCHAR(20),
s_name VARCHAR(20) NOT NULL DEFAULT ‘’,
s_birth VARCHAR(20) NOT NULL DEFAULT ‘’,
s_sex VARCHAR(10) NOT NULL DEFAULT ‘’,
PRIMARY KEY(s_id)
);
CREATE TABLE course(
c_id VARCHAR(20),
c_name VARCHAR(20) NOT NULL DEFAULT ‘’,
t_id VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id)
);
CREATE TABLE teacher(
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL DEFAULT ‘’,
PRIMARY KEY(t_id)
);
CREATE TABLE Score(
s_id VARCHAR(20),
c_id VARCHAR(20),
s_score INT(3),
PRIMARY KEY(s_id,c_id)
);
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 Teacher VALUES(‘01’ , ‘张三’);
INSERT INTO Teacher VALUES(‘02’ , ‘李四’);
INSERT INTO Teacher VALUES(‘03’ , ‘王五’);
#–成绩表测试数据
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(‘06’ , ‘01’ , 31);
INSERT INTO Score VALUES(‘06’ , ‘03’ , 34);
INSERT INTO Score VALUES(‘07’ , ‘02’ , 89);
INSERT INTO Score VALUES(‘07’ , ‘03’ , 98);
– 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT
st.*,
sc.s_score AS '语文',
sc2.s_score AS '数学'
FROM
student st
LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id='01'
LEFT JOIN score sc2 ON sc2.s_id=st.s_id AND sc2.c_id='02'
WHERE sc.s_score>sc2.s_score