注意:写sql语句最好用大写函数。
1、表结构创建语句解析
CREATE TABLE class(
cid INT not null primary key auto_increment,
caption varchar(32) not null
)engine = INNODB DEFAULT CHARSET=utf8;
engine = innobd 数据库试用innodb搜索引擎
DEFAULT CHARSET=utf8 数据库默认编码为utf8
经典数据库例题50道 https://blog.csdn.net/anthony_ju/article/details/81987868
1、查询学过编号为”01”但是没有学过编号为”02”的课程的同学的信息
select a.*,b.s_score as ‘01score’,c.s_score as ‘02score’
from Student a
inner join Score b on b.s_id = a.s_id and b.c_id = ‘01’
inner join Score c on c.s_id = a.s_id and c.c_id =‘02’
where b.s_score > c.s_score;
join的语法参照《inner join on》
as可以省略掉,换成 b.s_score 01score=b.s_score as ‘01score’
2、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select a.s_id,a.s_name,round(avg(s_score),1) as avg
from Student a
inner join Score b on b.s_id = a.s_id
group by b.s_id
having avg >=‘60’;
注意:round():把数字 字段舍入为指定小数位数。rount('字段,d)
d =0 表示没有小数点和小说部分。
avg()平均数。
3、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 – (包括有成绩的和无成绩的)
union 合并两个或多个 SELECT 语句的结果集(select语句必须有相同的列)
select a.s_id,a.s_name,round(avg(s_score),1) as avg
from Student a
inner join Score b on b.s_id = a.s_id
group by b.s_id
having avg <‘60’ UNION
select a.s_id,a.s_name,ROUND(AVG(s_score),1) as avg
from Student a
inner join Score b on b.s_id = a.s_id
group by b.s_id
having avg <‘60’ or avg is null;
union:参照详细函数应用。https://www.runoob.com/sql/sql-union.html
having:筛选出分组后的各组数据( group by having)
group by : 根据一个或多个列对结果集进行分组
4、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT a.s_id,a.s_name,count(b.c_id),SUM(b.s_score) as sc
FROM Student a
INNER JOIN Score b on a.s_id = b.s_id
GROUP BY a.s_id;
count() 返回匹配指定条件的值的数目(NULL不计入)
distinct :返回不重复的数目,没有重复的。
count()–所有记录
count(1)–所有记录
5、查询学过”张三”老师授课的同学的信息
SELECT Student. from Student
join Score on Student.s_id=Score.s_id
and Score.c_id
IN (SELECT Course.c_id from Course join Teacher ON Course.t_id=Teacher.t_id and Teacher.t_name=‘张三’);
IN :允许你在where子句中规定的多个值(操作符)用于查询某个范围的值。
查询必须是单列的值。
6、查询没学过”张三”老师授课的同学的信息
SELECT
*
FROM
student
WHERE
s_id NOT IN (
SELECT
student.s_id
FROM
student
join score on student.s_id =score.s_id
and score.c_id in (SELECT course.c_id from course join teacher on course.t_id=teacher.t_id and teacher.t_name=‘张三’));
7、查询学过编号为”01”并且也学过编号为”02”的课程的同学的信息
SELECT * FROM student a
join score b on a.s_id=b.s_id and b.c_id=‘01’
join score c on a.s_id=c.s_id and c.c_id=‘02’;
显示内链接:就是有inner join ,形成中间表为两个表经过on条件过滤后的笛卡儿积。
SELECT * FROM student a,score b,score c
where a.s_id=b.s_id and a.s_id=c.s_id and b.c_id='01’and c.c_id =‘02’;
隐式内链接 :没有inner join,通过where语句形成的中间表的笛卡儿积
SELECT a.* from student a
where a.s_id in(select s_id from score where c_id = ‘01’)
and a.s_id in (SELECT s_id FROM score where c_id =‘02’);
8、查询学过编号为”01”但是没有学过编号为”02”的课程的同学的信息
SELECT a.* from student a
where a.s_id in(SELECT s_id from score where c_id = ‘01’)
and a.s_id not in (SELECT s_id FROM score where c_id = ‘02’);
9、查询没有学全所有课程的同学的信息
先查询出学全所有课程的同学,再not in
SELECT * FROM student
where s_id not in (select a.s_id from student a,score b,score c,score d where a.s_id=b.s_id and a.s_id=c.s_id and a.s_id=d.s_id and b.c_id ='01’and c.c_id='02’and d.c_id=‘03’);
SELECT *
from student
where s_id
not in (SELECT s_id FROM score GROUP BY s_id having count(1)=(SELECT COUNT(1)from course));
10、查询至少有一门课与学号为”01”的同学所学相同的同学的信息
SELECT student.* from student
where s_id !=‘01’ and s_id in (
SELECT s_id from score where c_id in (
SELECT c_id from score where s_id=‘01’));