-- drop table if exists courses;
-- create table courses (
-- courseNo int(10) unsigned primary key auto_increment,
-- name varchar(10)
-- );
-- insert into courses values
-- ('1', '数据库'),
-- ('2', 'qtp'),
-- ('3', 'linux'),
-- ('4', '系统测试'),
-- ('5', '单元测试'),
-- ('6', '测试过程');
-- drop table if exists scores;
-- create table scores (
-- id int(10) unsigned primary key auto_increment,
-- courseNo int(10),
-- studentno varchar(10),
-- score tinyint(4)
-- );
-- insert into scores values
-- ('1', '1', '001', '90'),
-- ('2', '1', '002', '75'),
-- ('3', '2', '002', '98'),
-- ('4', '3', '001', '86'),
-- ('5', '3', '003', '80'),
-- ('6', '4', '004', '79'),
-- ('7', '5', '005', '96'),
-- ('8', '6', '006', '80');
-- 等值连接语法:
-- select * from 表1 inner join 表2 on 表1.列=表2.列
-- 左连接
-- select * from 表1 left join 表2 on 表1.列=表2.列
-- 右连接
-- select * from 表1 right join 表2 on 表1.列=表2.列
--
-- 查询学生信息及学生的成绩(两张表 学生表 成绩表)
-- select * from students inner join scores on students.studentNO=scores.studentno
-- 查询课程信息及课程的成绩(两张表 课程表 成绩表)
-- select * from courses inner join scores on courses.courseNo=scores.courseNo
-- 查询学生信息及学生的课程对应的成绩(三张表 学生表 课程表 成绩表)
-- select * from students inner join scores on students.studentno=scores.studentno
-- inner join courses on courses.courseno=scores.courseno
-- 查询王昭君的成绩,要求显示姓名、课程号、成绩(两张表 学生表 成绩表 (成绩表里面有课程号))
-- select students.name,scores.courseno,scores.score from students
-- inner join scores on students.studentno=scores.studentno
-- where students.name='王昭君'
-- 查询王昭君的数据库成绩,要求显示姓名、课程名、成绩(三张表 学生表 课程表 成绩表)
-- select students.name,courses.name,scores.score from students
-- inner join scores on students.studentno=scores.studentno
-- inner join courses on courses.courseno=scores.courseno
-- where students.name = '王昭君' and courses.name = '数据库'
-- 查询所有学生的数据库成绩,要求显示姓名、课程名、成绩(三张表 学生表 课程表 成绩表)
-- select students.name,courses.name,scores.score from students
-- inner join scores on students.studentno=scores.studentno
-- inner join courses on courses.courseno=scores.courseno
-- where courses.name = '数据库'
-- 查询男生中最高成绩,要求显示姓名、课程名、成绩(三张表 学生表 课程表 成绩表)
-- select students.name,courses.name,scores.score from students
-- inner join scores on students.studentno=scores.studentno
-- inner join courses on courses.courseno=scores.courseno
-- where students.sex='男' order by scores.score desc limit 1
-- 左连接
-- select * from 表1 left join 表2 on 表1.列=表2.列
-- 查询所有学生的成绩,包括没有成绩的学生(两张表 学生表 成绩表)
-- select * from students left join scores on students.studentNo=scores.studentno
-- 查询所有学生的成绩,包括没有成绩的学生,需要显示课程名(三张表 学生表 成绩表 课程表)
-- select * from students left join scores on students.studentNo=scores.studentno
-- left join courses on scores.courseNo=courses.courseNo
-- 右连接
-- select * from 表1 right join 表2 on 表1.列=表2.列
-- insert into courses values
-- (0, '语文'),
-- (0, '数学');
-- 查询所有课程的成绩,包括没有成绩的课程(两张表 课程表 成绩表)
-- select * from scores right join courses on scores.courseNo=courses.courseNo
-- 查询所有课程的成绩,包括没有成绩的课程,包括学生信息(三张表 课程表 成绩表 学生表)
-- select * from scores right join courses on scores.courseNo = courses.courseNo
-- left join students on scores.studentNo = students.studentNo
-- 子查询
-- 标量子查询
-- 查询大于平均年龄的学生 (学生表)
-- select * from students where age >(select avg(age) from students )
-- 查询王昭君数据库成绩,要求显示成绩(成绩表 学生表 课程表)
-- select score from scores where
-- courseNo=(select courseNo from courses where name='数据库')
-- and studentno=(select studentno from students where name='王昭君')
-- 查询王昭君的成绩,要求显示成绩(学生表 成绩表)
-- select * from scores where studentno=(select studentno from students where name ='王昭君')
-- 列级子查询
-- 查询18岁的学生的成绩,要求显示成绩 (成绩表 学生表 where in 列子查询 不能用= 用in )
-- select * from scores where studentno in (select studentno from students where age=18)
-- 行级子查询
-- 查询男生中年龄最大的学生信息(学生表)
-- select * from students where (sex,age)=(select sex,age from students where sex='男' order by age desc limit 1 )
-- 表级子查询
-- 查询数据库和系统测试的课程成绩 (课程表 成绩表)
-- select * from scores inner join(select * from courses where name in('数据库','系统测试')) courses on scores.courseNo=courses.courseNo
--
-- 子查询中特定关键字使用
--
-- in 范围
-- 格式: 主查询 where 条件 in (列子查询)
--
-- any | some 任意一个
-- 格式: 主查询 where 列 = any (列子查询)
-- 在条件查询的结果中匹配任意一个即可,等价于 in
--
-- all
-- 格式: 主查询 where 列 = all(列子查询) : 等于里面所有
-- 格式: 主查询 where 列 <>all(列子查询) : 不等一其中所有
-- 显示年龄在18到20岁学生信息
-- select * from students where age in (select age from students where age between 18 and 20)
-- create table courses (
-- courseNo int(10) unsigned primary key auto_increment,
-- name varchar(10)
-- );
-- insert into courses values
-- ('1', '数据库'),
-- ('2', 'qtp'),
-- ('3', 'linux'),
-- ('4', '系统测试'),
-- ('5', '单元测试'),
-- ('6', '测试过程');
-- drop table if exists scores;
-- create table scores (
-- id int(10) unsigned primary key auto_increment,
-- courseNo int(10),
-- studentno varchar(10),
-- score tinyint(4)
-- );
-- insert into scores values
-- ('1', '1', '001', '90'),
-- ('2', '1', '002', '75'),
-- ('3', '2', '002', '98'),
-- ('4', '3', '001', '86'),
-- ('5', '3', '003', '80'),
-- ('6', '4', '004', '79'),
-- ('7', '5', '005', '96'),
-- ('8', '6', '006', '80');
-- 等值连接语法:
-- select * from 表1 inner join 表2 on 表1.列=表2.列
-- 左连接
-- select * from 表1 left join 表2 on 表1.列=表2.列
-- 右连接
-- select * from 表1 right join 表2 on 表1.列=表2.列
--
-- 查询学生信息及学生的成绩(两张表 学生表 成绩表)
-- select * from students inner join scores on students.studentNO=scores.studentno
-- 查询课程信息及课程的成绩(两张表 课程表 成绩表)
-- select * from courses inner join scores on courses.courseNo=scores.courseNo
-- 查询学生信息及学生的课程对应的成绩(三张表 学生表 课程表 成绩表)
-- select * from students inner join scores on students.studentno=scores.studentno
-- inner join courses on courses.courseno=scores.courseno
-- 查询王昭君的成绩,要求显示姓名、课程号、成绩(两张表 学生表 成绩表 (成绩表里面有课程号))
-- select students.name,scores.courseno,scores.score from students
-- inner join scores on students.studentno=scores.studentno
-- where students.name='王昭君'
-- 查询王昭君的数据库成绩,要求显示姓名、课程名、成绩(三张表 学生表 课程表 成绩表)
-- select students.name,courses.name,scores.score from students
-- inner join scores on students.studentno=scores.studentno
-- inner join courses on courses.courseno=scores.courseno
-- where students.name = '王昭君' and courses.name = '数据库'
-- 查询所有学生的数据库成绩,要求显示姓名、课程名、成绩(三张表 学生表 课程表 成绩表)
-- select students.name,courses.name,scores.score from students
-- inner join scores on students.studentno=scores.studentno
-- inner join courses on courses.courseno=scores.courseno
-- where courses.name = '数据库'
-- 查询男生中最高成绩,要求显示姓名、课程名、成绩(三张表 学生表 课程表 成绩表)
-- select students.name,courses.name,scores.score from students
-- inner join scores on students.studentno=scores.studentno
-- inner join courses on courses.courseno=scores.courseno
-- where students.sex='男' order by scores.score desc limit 1
-- 左连接
-- select * from 表1 left join 表2 on 表1.列=表2.列
-- 查询所有学生的成绩,包括没有成绩的学生(两张表 学生表 成绩表)
-- select * from students left join scores on students.studentNo=scores.studentno
-- 查询所有学生的成绩,包括没有成绩的学生,需要显示课程名(三张表 学生表 成绩表 课程表)
-- select * from students left join scores on students.studentNo=scores.studentno
-- left join courses on scores.courseNo=courses.courseNo
-- 右连接
-- select * from 表1 right join 表2 on 表1.列=表2.列
-- insert into courses values
-- (0, '语文'),
-- (0, '数学');
-- 查询所有课程的成绩,包括没有成绩的课程(两张表 课程表 成绩表)
-- select * from scores right join courses on scores.courseNo=courses.courseNo
-- 查询所有课程的成绩,包括没有成绩的课程,包括学生信息(三张表 课程表 成绩表 学生表)
-- select * from scores right join courses on scores.courseNo = courses.courseNo
-- left join students on scores.studentNo = students.studentNo
-- 子查询
-- 标量子查询
-- 查询大于平均年龄的学生 (学生表)
-- select * from students where age >(select avg(age) from students )
-- 查询王昭君数据库成绩,要求显示成绩(成绩表 学生表 课程表)
-- select score from scores where
-- courseNo=(select courseNo from courses where name='数据库')
-- and studentno=(select studentno from students where name='王昭君')
-- 查询王昭君的成绩,要求显示成绩(学生表 成绩表)
-- select * from scores where studentno=(select studentno from students where name ='王昭君')
-- 列级子查询
-- 查询18岁的学生的成绩,要求显示成绩 (成绩表 学生表 where in 列子查询 不能用= 用in )
-- select * from scores where studentno in (select studentno from students where age=18)
-- 行级子查询
-- 查询男生中年龄最大的学生信息(学生表)
-- select * from students where (sex,age)=(select sex,age from students where sex='男' order by age desc limit 1 )
-- 表级子查询
-- 查询数据库和系统测试的课程成绩 (课程表 成绩表)
-- select * from scores inner join(select * from courses where name in('数据库','系统测试')) courses on scores.courseNo=courses.courseNo
--
-- 子查询中特定关键字使用
--
-- in 范围
-- 格式: 主查询 where 条件 in (列子查询)
--
-- any | some 任意一个
-- 格式: 主查询 where 列 = any (列子查询)
-- 在条件查询的结果中匹配任意一个即可,等价于 in
--
-- all
-- 格式: 主查询 where 列 = all(列子查询) : 等于里面所有
-- 格式: 主查询 where 列 <>all(列子查询) : 不等一其中所有
-- 显示年龄在18到20岁学生信息
-- select * from students where age in (select age from students where age between 18 and 20)