MySQL 多表查询

测试表

--建表--学生表CREATETABLE`Student`(`s_id`VARCHAR(20),`s_name`VARCHAR(20)NOTNULLDEFAULT'',`s_birth`VARCHAR(20)NOTNULLDEFAULT'',`s_sex`VARCHAR(10)NOTNULLDEFAULT'',PRIMARYKEY(`s_id`));--课程表CREATETABLE`Course`(`c_id`VARCHAR(20),`c_name`VARCHAR(20)NOTNULLDEFAULT'',`t_id`VARCHAR(20)NOTNULL,PRIMARYKEY(`c_id`));--教师表CREATETABLE`Teacher`(`t_id`VARCHAR(20),`t_name`VARCHAR(20)NOTNULLDEFAULT'',PRIMARYKEY(`t_id`));--成绩表CREATETABLE`Score`(`s_id`VARCHAR(20),`c_id`VARCHAR(20),`s_score`INT(3),PRIMARYKEY(`s_id`,`c_id`));--插入学生表测试数据insertintoStudentvalues('01','赵雷','1990-01-01','男');insertintoStudentvalues('02','钱电','1990-12-21','男');insertintoStudentvalues('03','孙风','1990-05-20','男');insertintoStudentvalues('04','李云','1990-08-06','男');insertintoStudentvalues('05','周梅','1991-12-01','女');insertintoStudentvalues('06','吴兰','1992-03-01','女');insertintoStudentvalues('07','郑竹','1989-07-01','女');insertintoStudentvalues('08','王菊','1990-01-20','女');--课程表测试数据insertintoCoursevalues('01','语文','02');insertintoCoursevalues('02','数学','01');insertintoCoursevalues('03','英语','03');--教师表测试数据insertintoTeachervalues('01','张三');insertintoTeachervalues('02','李四');insertintoTeachervalues('03','王五');--成绩表测试数据insertintoScorevalues('01','01',80);insertintoScorevalues('01','02',90);insertintoScorevalues('01','03',99);insertintoScorevalues('02','01',70);insertintoScorevalues('02','02',60);insertintoScorevalues('02','03',80);insertintoScorevalues('03','01',80);insertintoScorevalues('03','02',80);insertintoScorevalues('03','03',80);insertintoScorevalues('04','01',50);insertintoScorevalues('04','02',30);insertintoScorevalues('04','03',20);insertintoScorevalues('05','01',76);insertintoScorevalues('05','02',87);insertintoScorevalues('06','01',31);insertintoScorevalues('06','03',34);insertintoScorevalues('07','02',89);insertintoScorevalues('07','03',98);

练习题

1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

select s.*,s1.s_score,s2.s_score,c.c_name from student as s

INNER JOIN score as s1 on s.s_id = s1.s_id and s1.c_id='01'

INNER JOIN score as s2 on s.s_id = s2.s_id and s2.c_id='02'

INNER JOIN course as c on s1.c_id = c.c_id

where s1.s_score > s2.s_score

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

select s.*,s1.s_score,s2.s_score,c.c_name from student as s

INNER JOIN score as s1 on s.s_id = s1.s_id and s1.c_id='01'

INNER JOIN score as s2 on s.s_id = s2.s_id and s2.c_id='02'

INNER JOIN course as c on s1.c_id = c.c_id

where s1.s_score < s2.s_score

3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

select s.s_id,s.s_name, ROUND(AVG(b.s_score),2) from student as s

INNER JOIN score as b on b.s_id = s.s_id

GROUP BY s.s_id,s.s_name HAVING ROUND(AVG(b.s_score),2) > 60

4、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

        count()函数里面的参数是列名的的时候,那么会计算有值项的次数。

        Sum()函数里面的参数是列名的时候,是计算列名的值的相加,而不是有值项的总数。

select s.s_id,s.s_name,count(a.c_id),SUM(a.s_score) from student as s

INNER JOIN score as a on a.s_id=s.s_id

GROUP BY s.s_id,s.s_name

-- 6、查询"李"姓老师的数量 

select count(t_id) from teacher where t_name like '李%';

- 7、查询学过"张三"老师授课的同学的信息

select s.*,a.*,b.*,c.* from student as s

INNER JOIN score as a on s.s_id = a.s_id

INNER JOIN course as b on a.c_id = b.c_id

INNER JOIN teacher as c on b.t_id = c.t_id

where c.t_name = '张三'

-- 8、查询没学过"张三"老师授课的同学的信息

select * from student as f where f.s_id not in

(select s.s_id from student as s

INNER JOIN score as a on s.s_id = a.s_id

INNER JOIN course as b on a.c_id = b.c_id

INNER JOIN teacher as c on b.t_id = c.t_id

where c.t_name = '张三')

-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

select s.* from student as s

INNER JOIN score as a1 on a1.s_id = s.s_id

AND a1.c_id='01'

INNER JOIN score as a2 on a2.s_id = s.s_id

AND a2.c_id='02'

-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

SELECT s.* from student as s where s.s_id

in (SELECT s_id from score where c_id = "01")

and s.s_id not in (SELECT s_id from score where c_id = "02")

11、查询没有学全所有课程的同学的信息

select * from student as s where s.s_id in (

select score.s_id from score where score.s_id not in(

select a.s_id from score a

join score b on a.s_id = b.s_id and b.c_id='02'

join score c on a.s_id = c.s_id and c.c_id='03'

where a.c_id="01"

))

-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

select * from student where s_id in (

select s_id from score where c_id in(

SELECT c_id from score where s_id="01"

)

你可能感兴趣的:(MySQL 多表查询)