2020.8.14课堂笔记(MySQL练习)

1、查询语法结构
select [distinct] {*|column_name [as 别名] [,…]}
from table_name [as 别名];
2、连接有哪几种类型,有什么区别
内连接:inner join 其中inner可以省略,符合关联条件的才显示
等值连接:条件是等量关系
非等值连接:连接条件中的关系是非等量关系
自连接:将一张表看作两张表,自己连自己
外连接:主表的数据全显示,附表只显示符合关联条件的数据
左外连接:left join,以左边的表为主表,关联右边的表
右外连接:right join,以右边的表为主表,关联左边的表
3、a拉链表中有id,name,pid字段,其中pid为父级id,那么请写出对应sql,
展示父栏目名称和对应子栏目名称
select a2.name ‘父栏目名称’ a1.name ‘子栏目名称’
from a a1
join a a2
on a1.pid=a2.id;

mysql> select * from result;
±-------±-------±--------------------±------+
| stu_id | sub_no | exam_date | score |
±-------±-------±--------------------±------+
| 1012 | 3 | 2020-08-13 14:36:47 | 100 |
| 1013 | 3 | 2020-08-13 14:39:38 | 87 |
| 1012 | 3 | 2020-08-13 14:39:38 | 100 |
| 1013 | 4 | 2020-08-13 14:39:38 | 89 |
| 1012 | 3 | 2020-08-13 14:39:38 | 58 |
| 1012 | 4 | 2020-08-13 14:39:38 | 96 |
| 1012 | 2 | 2020-08-13 14:39:38 | 90 |
| 1013 | 4 | 2020-08-13 14:39:38 | 95 |
| 1013 | 2 | 2020-08-13 14:39:38 | 65 |
| 1012 | 2 | 2020-08-13 14:39:38 | 64 |
| 1012 | 2 | 2020-08-13 14:39:38 | 54 |
| 1013 | 3 | 2020-08-13 14:39:38 | 73 |
| 1020 | 1 | 2020-08-13 17:16:06 | 85 |
±-------±-------±--------------------±------+
13 rows in set (0.00 sec)

mysql> select * from result order by sut_id;
ERROR 1054 (42S22): Unknown column ‘sut_id’ in ‘order clause’
mysql> select * from result order by stu_id;
±-------±-------±--------------------±------+
| stu_id | sub_no | exam_date | score |
±-------±-------±--------------------±------+
| 1012 | 3 | 2020-08-13 14:36:47 | 100 |
| 1012 | 2 | 2020-08-13 14:39:38 | 54 |
| 1012 | 2 | 2020-08-13 14:39:38 | 64 |
| 1012 | 2 | 2020-08-13 14:39:38 | 90 |
| 1012 | 4 | 2020-08-13 14:39:38 | 96 |
| 1012 | 3 | 2020-08-13 14:39:38 | 58 |
| 1012 | 3 | 2020-08-13 14:39:38 | 100 |
| 1013 | 4 | 2020-08-13 14:39:38 | 89 |
| 1013 | 4 | 2020-08-13 14:39:38 | 95 |
| 1013 | 2 | 2020-08-13 14:39:38 | 65 |
| 1013 | 3 | 2020-08-13 14:39:38 | 87 |
| 1013 | 3 | 2020-08-13 14:39:38 | 73 |
| 1020 | 1 | 2020-08-13 17:16:06 | 85 |
±-------±-------±--------------------±------+
13 rows in set (0.00 sec)

mysql> select * from result order by stu_id,sub_no;
±-------±-------±--------------------±------+
| stu_id | sub_no | exam_date | score |
±-------±-------±--------------------±------+
| 1012 | 2 | 2020-08-13 14:39:38 | 90 |
| 1012 | 2 | 2020-08-13 14:39:38 | 54 |
| 1012 | 2 | 2020-08-13 14:39:38 | 64 |
| 1012 | 3 | 2020-08-13 14:36:47 | 100 |
| 1012 | 3 | 2020-08-13 14:39:38 | 58 |
| 1012 | 3 | 2020-08-13 14:39:38 | 100 |
| 1012 | 4 | 2020-08-13 14:39:38 | 96 |
| 1013 | 2 | 2020-08-13 14:39:38 | 65 |
| 1013 | 3 | 2020-08-13 14:39:38 | 87 |
| 1013 | 3 | 2020-08-13 14:39:38 | 73 |
| 1013 | 4 | 2020-08-13 14:39:38 | 89 |
| 1013 | 4 | 2020-08-13 14:39:38 | 95 |
| 1020 | 1 | 2020-08-13 17:16:06 | 85 |
±-------±-------±--------------------±------+
13 rows in set (0.00 sec)

mysql> select * from result order by score desc;
±-------±-------±--------------------±------+
| stu_id | sub_no | exam_date | score |
±-------±-------±--------------------±------+
| 1012 | 3 | 2020-08-13 14:36:47 | 100 |
| 1012 | 3 | 2020-08-13 14:39:38 | 100 |
| 1012 | 4 | 2020-08-13 14:39:38 | 96 |
| 1013 | 4 | 2020-08-13 14:39:38 | 95 |
| 1012 | 2 | 2020-08-13 14:39:38 | 90 |
| 1013 | 4 | 2020-08-13 14:39:38 | 89 |
| 1013 | 3 | 2020-08-13 14:39:38 | 87 |
| 1020 | 1 | 2020-08-13 17:16:06 | 85 |
| 1013 | 3 | 2020-08-13 14:39:38 | 73 |
| 1013 | 2 | 2020-08-13 14:39:38 | 65 |
| 1012 | 2 | 2020-08-13 14:39:38 | 64 |
| 1012 | 3 | 2020-08-13 14:39:38 | 58 |
| 1012 | 2 | 2020-08-13 14:39:38 | 54 |
±-------±-------±--------------------±------+
13 rows in set (0.00 sec)

mysql> select * from result order by exam_date desc;
±-------±-------±--------------------±------+
| stu_id | sub_no | exam_date | score |
±-------±-------±--------------------±------+
| 1020 | 1 | 2020-08-13 17:16:06 | 85 |
| 1013 | 3 | 2020-08-13 14:39:38 | 73 |
| 1012 | 2 | 2020-08-13 14:39:38 | 54 |
| 1012 | 2 | 2020-08-13 14:39:38 | 64 |
| 1013 | 2 | 2020-08-13 14:39:38 | 65 |
| 1013 | 4 | 2020-08-13 14:39:38 | 95 |
| 1012 | 2 | 2020-08-13 14:39:38 | 90 |
| 1012 | 4 | 2020-08-13 14:39:38 | 96 |
| 1012 | 3 | 2020-08-13 14:39:38 | 58 |
| 1013 | 4 | 2020-08-13 14:39:38 | 89 |
| 1012 | 3 | 2020-08-13 14:39:38 | 100 |
| 1013 | 3 | 2020-08-13 14:39:38 | 87 |
| 1012 | 3 | 2020-08-13 14:36:47 | 100 |
±-------±-------±--------------------±------+
13 rows in set (0.00 sec)

mysql> select * from result order by sub_no desc,score desc;
±-------±-------±--------------------±------+
| stu_id | sub_no | exam_date | score |
±-------±-------±--------------------±------+
| 1012 | 4 | 2020-08-13 14:39:38 | 96 |
| 1013 | 4 | 2020-08-13 14:39:38 | 95 |
| 1013 | 4 | 2020-08-13 14:39:38 | 89 |
| 1012 | 3 | 2020-08-13 14:36:47 | 100 |
| 1012 | 3 | 2020-08-13 14:39:38 | 100 |
| 1013 | 3 | 2020-08-13 14:39:38 | 87 |
| 1013 | 3 | 2020-08-13 14:39:38 | 73 |
| 1012 | 3 | 2020-08-13 14:39:38 | 58 |
| 1012 | 2 | 2020-08-13 14:39:38 | 90 |
| 1013 | 2 | 2020-08-13 14:39:38 | 65 |
| 1012 | 2 | 2020-08-13 14:39:38 | 64 |
| 1012 | 2 | 2020-08-13 14:39:38 | 54 |
| 1020 | 1 | 2020-08-13 17:16:06 | 85 |
±-------±-------±--------------------±------+
13 rows in set (0.00 sec)

mysql> select * from result order by score desc limit 3;
±-------±-------±--------------------±------+
| stu_id | sub_no | exam_date | score |
±-------±-------±--------------------±------+
| 1012 | 3 | 2020-08-13 14:36:47 | 100 |
| 1012 | 3 | 2020-08-13 14:39:38 | 100 |
| 1012 | 4 | 2020-08-13 14:39:38 | 96 |
±-------±-------±--------------------±------+
3 rows in set (0.00 sec)

mysql> select * from result order by score desc limit 4,6;
±-------±-------±--------------------±------+
| stu_id | sub_no | exam_date | score |
±-------±-------±--------------------±------+
| 1012 | 2 | 2020-08-13 14:39:38 | 90 |
| 1013 | 4 | 2020-08-13 14:39:38 | 89 |
| 1013 | 3 | 2020-08-13 14:39:38 | 87 |
| 1020 | 1 | 2020-08-13 17:16:06 | 85 |
| 1013 | 3 | 2020-08-13 14:39:38 | 73 |
| 1013 | 2 | 2020-08-13 14:39:38 | 65 |
±-------±-------±--------------------±------+
6 rows in set (0.00 sec)

默认asc 倒序desc

mysql> select SubjectName,score from subject join result on SubjectNo=sub_no whe desc;
±---------------±------+
| SubjectName | score |
±---------------±------+
| 高等数学-2 | 90 |
| 高等数学-2 | 65 |
| 高等数学-2 | 64 |
| 高等数学-2 | 54 |
±---------------±------+
4 rows in set (0.00 sec)

mysql> mysql> * from result order by score desc limit 6 offset 4;
±-------±-------±--------------------±------+
| stu_id | sub_no | exam_date | score |
±-------±-------±--------------------±------+
| 1012 | 2 | 2020-08-13 14:39:38 | 90 |
| 1013 | 4 | 2020-08-13 14:39:38 | 89 |
| 1013 | 3 | 2020-08-13 14:39:38 | 87 |
| 1020 | 1 | 2020-08-13 17:16:06 | 85 |
| 1013 | 3 | 2020-08-13 14:39:38 | 73 |
| 1013 | 2 | 2020-08-13 14:39:38 | 65 |
±-------±-------±--------------------±------+
6 rows in set (0.00 sec)

把起始位置加到后面去 效果和 limit(4,6)一样的

查询出李某某同学小于80的分数,按从低到高排列

mysql> select r.score from result r
-> join student s
-> on s.stu_id=r.stu_id
-> where s.stu_name like “李%” and r.score<80 order by r.score;
±------+
| score |
±------+
| 54 |
| 58 |
| 64 |
| 65 |
| 73 |
±------+
5 rows in set (0.00 sec)

mysql> select r.* from result r join student s on r.stu_id=s.stu_id
-> where s.stu_name like ‘李__’ order by score;
±-------±-------±--------------------±------+
| stu_id | sub_no | exam_date | score |
±-------±-------±--------------------±------+
| 1012 | 2 | 2020-08-13 14:39:38 | 54 |
| 1012 | 3 | 2020-08-13 14:39:38 | 58 |
| 1012 | 2 | 2020-08-13 14:39:38 | 64 |
| 1012 | 2 | 2020-08-13 14:39:38 | 90 |
| 1012 | 4 | 2020-08-13 14:39:38 | 96 |
| 1012 | 3 | 2020-08-13 14:36:47 | 100 |
| 1012 | 3 | 2020-08-13 14:39:38 | 100 |
±-------±-------±--------------------±------+
7 rows in set (0.00 sec)

mysql> select r.* from result r join student s on r.stu_id=s.stu_id where s.stu_name like ‘李__’ and score<80 order by score;
±-------±-------±--------------------±------+
| stu_id | sub_no | exam_date | score |
±-------±-------±--------------------±------+
| 1012 | 2 | 2020-08-13 14:39:38 | 54 |
| 1012 | 3 | 2020-08-13 14:39:38 | 58 |
| 1012 | 2 | 2020-08-13 14:39:38 | 64 |
±-------±-------±--------------------±------+
3 rows in set (0.00 sec)

mysql> select r.stu_id,s.stu_name,sub.subjectName,score from result r
-> join student s on r.stu_id=s.stu_id
-> join subject sub on r.sub_no=sub.subjectNo
-> where subjectName=‘高等数学-2’;
±-------±----------±---------------±------+
| stu_id | stu_name | subjectName | score |
±-------±----------±---------------±------+
| 1012 | 李文才 | 高等数学-2 | 90 |
| 1013 | 李梅 | 高等数学-2 | 65 |
| 1012 | 李文才 | 高等数学-2 | 64 |
| 1012 | 李文才 | 高等数学-2 | 54 |
±-------±----------±---------------±------+
4 rows in set (0.00 sec)

mysql> select r.stu_id,s.stu_name,sub.subjectName,score from result r join student s on r.stu_id=s.stu_id join subject sub on r.sub_no=sub.subjectNo where subjectName=‘高等数学-2’ order by score desc limit 2;
±-------±----------±---------------±------+
| stu_id | stu_name | subjectName | score |
±-------±----------±---------------±------+
| 1012 | 李文才 | 高等数学-2 | 90 |
| 1013 | 李梅 | 高等数学-2 | 65 |
±-------±----------±---------------±------+
2 rows in set (0.00 sec)

mysql> select r.stu_id,s.stu_name,sub.subjectName,score from result r join student s on r.stu_id=s.stu_id join subject sub on r.sub_no=sub.subjectNo where subjectName=‘高等数学-2’ order by score desc limit (2,2);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘(2,2)’ at line 1
mysql> select r.stu_id,s.stu_name,sub.subjectName,score from result r join student s on r.stu_id=s.stu_id join subject sub on r.sub_no=sub.subjectNo where subjectName=‘高等数学-2’ order by score desc limit 2,2;
±-------±----------±---------------±------+
| stu_id | stu_name | subjectName | score |
±-------±----------±---------------±------+
| 1012 | 李文才 | 高等数学-2 | 64 |
| 1012 | 李文才 | 高等数学-2 | 54 |
±-------±----------±---------------±------+
2 rows in set (0.00 sec)

select r.stu_id ‘学号’,s.stu_name ‘姓名’,sub.subjectName ‘课程名’,score ‘分数’
from result r
join student s on r.stu_id=s.stu_id
join subject sub on r.sub_no=sub.subjectNo
where sub.subjectName=‘高等数学-2’ and score>80 order by score limit 10;

2,3,4,10远不是看起来这么简单13左外关联出现为空的情况,11,13,16,18,19,20,
22,24,26.27,28,30,

#2、查询平均成绩大于95分的同学的学号和平均成绩;
SELECT s.stu_id ‘学号’,AVG(scores) avg_s FROM tb_score s
GROUP BY stu_id
HAVING avg_s > 95;

#3.查询所有同学的学号、姓名、选课数、总成绩;
SELECT s.stu_id ‘学号’,stu.sname ‘姓名’,COUNT(DISTINCT corse_id),SUM(scores) sum_s FROM tb_score s
JOIN tb_student stu ON stu.sid=s.stu_id
GROUP BY s.stu_id;

#4.查询姓“李”的老师的个数;
SELECT COUNT(1) FROM tb_teacher WHERE tname LIKE ‘李%’;

#10.查询没有学全所有课的同学的学号、姓名;
SELECT s.sid ‘学号’,s.sname ‘姓名’ FROM tb_score sc
JOIN tb_student s ON s.sid=sc.stu_id
GROUP BY sc.stu_id
HAVING COUNT(corse_id)<6;

#11.查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
SELECT s.sid ‘学号’,s.sname ‘姓名’ FROM tb_score sc
JOIN tb_student s ON s.sid=sc.stu_id
WHERE corse_id IN (SELECT DISTINCT corse_id FROM tb_score WHERE stu_id=1) AND s.sid<>1
GROUP BY sc.stu_id;

#13.查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;

SELECT DISTINCT a.stu_id,COUNT(a.corse_id) ‘相同课程个数’ FROM tb_score a
LEFT JOIN
(SELECT corse_id FROM tb_score WHERE stu_id=2) b
ON a.corse_id=b.corse_id
GROUP BY stu_id
HAVING 相同课程个数=(SELECT COUNT(corse_id) FROM tb_score WHERE stu_id=2) AND stu_id<>2

SELECT corse_id FROM tb_score WHERE stu_id=2

SELECT corse_id FROM tb_score
GROUP BY stu_id
HAVING COUNT(DISTINCT corse_id)=(SELECT COUNT(DISTINCT corse_id) FROM tb_score WHERE stu_id=2) AND stu_id<>2

#16、按平均成绩从低到高显示所有学生的“java”、“mysql”、“hadoop”三门的课程成绩,
#按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
SELECT s.stu_id ‘学生ID’,
(SELECT scores FROM tb_score WHERE s.stu_id=stu_id AND corse_id=1) ‘java’,
(SELECT scores FROM tb_score WHERE s.stu_id=stu_id AND corse_id=3) ‘mysql’,
(SELECT scores FROM tb_score WHERE s.stu_id=stu_id AND corse_id=4) ‘hadoop’,
COUNT(corse_id) 有效课程数 ,AVG(scores) 平均分 FROM tb_score s
GROUP BY s.stu_id
ORDER BY 平均分 DESC;

#18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
#思路:通过课程id来进行分组,这个时候会显示四行,然后用聚合函数max,min来找出最大值和最小值。

SELECT corse_id ‘课程id’ ,MAX(scores) ‘最高分’, MIN(scores) ‘最低分’ FROM tb_score
GROUP BY corse_id;

#19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
#新知识点:case when then相当于if判断

SELECT s1.corse_id ‘课程id’, AVG(s1.scores) ‘平均分’ ,s2.count1/COUNT(s1.stu_id) ‘及格率’ FROM tb_score s1
JOIN
(SELECT corse_id,COUNT(stu_id) count1 FROM tb_score
WHERE scores>90
GROUP BY corse_id) s2
ON s1.corse_id=s2.corse_id
GROUP BY s1.corse_id
ORDER BY ‘平均分’ DESC,‘及格率’ DESC;

#20、课程平均分从高到低显示(显示任课老师);
SELECT c.cname,AVG(scores),t.tname FROM tb_score s
JOIN tb_course c ON c.cid=s.corse_id
JOIN tb_teacher t ON t.tid=c.teacher_id
GROUP BY corse_id;

#22、查询每门课程被选修的学生数;
SELECT c.cname,COUNT(stu_id) FROM tb_score s
JOIN tb_course c ON c.cid=s.corse_id
GROUP BY corse_id;

#23、查询出只选修了一门课程的全部学生的学号和姓名;
SELECT s.sid,s.sname FROM tb_student s
#join tb_score sc on sc.stu_id=s.sid
JOIN(
SELECT s.stu_id stu_a FROM tb_score s
JOIN tb_student stu ON stu.sid=s.stu_id
GROUP BY stu_id
HAVING COUNT(corse_id)=1
) a ON a.stu_a=s.sid
GROUP BY s.sid;

#24、查询男生、女生的人数;
SELECT gender,COUNT(gender) FROM tb_student GROUP BY gender DESC;

#26、查询同名同姓学生名单,并统计同名人数;
SELECT sname,COUNT(sid) FROM tb_student
GROUP BY sname
HAVING COUNT(sid)>1

#27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
SELECT corse_id,AVG(scores) FROM tb_score
GROUP BY corse_id
ORDER BY AVG(scores),corse_id DESC;

#28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;
SELECT s.stu_id ‘学号’,stu.sname ‘姓名’,AVG(scores) ‘平均成绩’ FROM tb_score s
JOIN tb_student stu ON stu.sid=s.stu_id
GROUP BY s.stu_id
HAVING 平均成绩 >95;

#30、求选了课程的学生人数
SELECT COUNT(DISTINCT stu_id) FROM tb_score
WHERE stu_id IN (SELECT stu_id FROM tb_score GROUP BY stu_id HAVING COUNT(corse_id)>0 )

你可能感兴趣的:(笔记)