……继上一篇MySQL的开发总结之后,适当的练习还是很有必要的……
SQL语法多变,不敢保证唯一,也不敢保证全对,如果错误欢迎指出,即刻修改。
一、现有表结构如下图
TABLENAME:afinfo
Id |
name |
age |
birth |
sex |
memo |
1 |
徐洪国 |
37 |
1979-03-23 |
男 |
高中 |
2 |
王芳 |
26 |
1988-02-06 |
女 |
本科 |
3 |
李达康 |
24 |
1990-04-02 |
男 |
硕士 |
4 |
侯亮平 |
30 |
1984-09-12 |
女 |
博士 |
5 |
徐夫子 |
27 |
1987-12-30 |
男 |
大专 |
6 |
…… |
…… |
…… |
…… |
…… |
1)请编写sql语句对年龄进行升序排列
1 2 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span> * <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> afinfo </span>-> order by birth;</span> |
2)请编写sql语句查询对“徐”姓开头的人员名单
1 2 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span> * <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> afinfo </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> name like <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">徐%</span><span style="color: rgba(128, 0, 0, 1);">'</span>;</span> |
3)请编写sql语句修改“李达康”的年龄为“45”
1 2 3 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> update afinfo </span>-> <span style="color: rgba(0, 0, 255, 1);">set</span> age=<span style="color: rgba(128, 0, 128, 1);">45</span> -> <span style="color: rgba(0, 0, 255, 1);">where</span> name=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">李达康</span><span style="color: rgba(128, 0, 0, 1);">'</span>;</span> |
4)请编写sql删除王芳这表数据记录。
1 2 |
<span style="font-size: 16px;">mysql> delete <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> afinfo </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> name=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">王芳</span><span style="color: rgba(128, 0, 0, 1);">'</span>;</span> |
二、现有以下学生表和考试信息表
学生信息表(student)
姓名name |
学号code |
张三 |
001 |
李四 |
002 |
马五 |
003 |
甲六 |
004 |
考试信息表(exam)
学号code |
学科subject |
成绩score |
001 |
数学 |
80 |
002 |
数学 |
75 |
001 |
语文 |
90 |
002 |
语文 |
80 |
001 |
英语 |
90 |
002 |
英语 |
85 |
003 |
英语 |
80 |
004 |
英语 |
70 |
1)查询出所有学生信息,SQL怎么编写?
1 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span> * <span style="color: rgba(0, 0, 255, 1);">from</span> student;</span> |
2)新学生小明,学号为005,需要将信息写入学生信息表,SQL语句怎么编写?
1 |
<span style="font-size: 16px;">mysql> insert into student values(<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">小明</span><span style="color: rgba(128, 0, 0, 1);">'</span>,<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">005</span><span style="color: rgba(128, 0, 0, 1);">'</span>);</span> |
3)李四语文成绩被登记错误,成绩实际为85分,更新到考试信息表中,SQL语句怎么编写?
1 2 3 4 5 |
<span style="font-size: 16px;">mysql><span style="color: rgba(0, 0, 0, 1);"> update exam,student </span>-> <span style="color: rgba(0, 0, 255, 1);">set</span> exam.score=<span style="color: rgba(128, 0, 128, 1);">85</span> -> <span style="color: rgba(0, 0, 255, 1);">where</span> student.code=<span style="color: rgba(0, 0, 0, 1);">exam.code </span>-> and student.name=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">李四</span><span style="color: rgba(128, 0, 0, 1);">'</span> -> and exam.subject=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">语文</span><span style="color: rgba(128, 0, 0, 1);">'</span>;</span> |
4)查询出各科成绩的平均成绩,显示字段为:学科、平均分,SQL怎么编写?
1 2 3 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> subject 学科,avg(score) 平均分 </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> exam </span>-> group by subject;</span> |
5)查询出所有学生各科成绩,显示字段为:姓名、学号、学科、成绩,并以学号与学科排序,没有成绩的学生也需要列出,SQL怎么编写?
1 2 3 4 5 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> s.name 姓名,s.code 学号,e.subject 学科,e.score 成绩 </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student s </span>-><span style="color: rgba(0, 0, 0, 1);"> left join exam e </span>-> on s.code=<span style="color: rgba(0, 0, 0, 1);">e.code </span>-> order by 学号,学科;</span> |
6)查询出单科成绩最高的,显示字段为:姓名、学号、学科、成绩,SQL怎么编写?
1 2 3 4 5 6 7 8 9 10 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> s.name 姓名,s.code 学号,e.subject 学科,e.score 成绩 </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student s </span>-><span style="color: rgba(0, 0, 0, 1);"> join exam e </span>-> on s.code=<span style="color: rgba(0, 0, 0, 1);">e.code </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> (e.subject,e.score) <span style="color: rgba(0, 0, 255, 1);">in</span> -><span style="color: rgba(0, 0, 0, 1);"> ( </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> subject,max(score) </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> exam </span>-><span style="color: rgba(0, 0, 0, 1);"> group by subject </span>-> );</span> |
7)列出每位学生的各科成绩,要求输出格式:姓名、学号、语文成绩、数学成绩、英语成绩,SQL怎么编写?
1 2 3 4 5 6 7 8 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> s.name 姓名,s.code 学号, </span>-> sum(<span style="color: rgba(0, 0, 255, 1);">if</span>(e.subject=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">语文</span><span style="color: rgba(128, 0, 0, 1);">'</span>,e.score,<span style="color: rgba(128, 0, 128, 1);">0</span><span style="color: rgba(0, 0, 0, 1);">)) 语文成绩, </span>-> sum(<span style="color: rgba(0, 0, 255, 1);">if</span>(e.subject=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">数学</span><span style="color: rgba(128, 0, 0, 1);">'</span>,e.score,<span style="color: rgba(128, 0, 128, 1);">0</span><span style="color: rgba(0, 0, 0, 1);">)) 数学成绩, </span>-> sum(<span style="color: rgba(0, 0, 255, 1);">if</span>(e.subject=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">英语</span><span style="color: rgba(128, 0, 0, 1);">'</span>,e.score,<span style="color: rgba(128, 0, 128, 1);">0</span><span style="color: rgba(0, 0, 0, 1);">)) 英语成绩 </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student s </span>-><span style="color: rgba(0, 0, 0, 1);"> left join exam e </span>-> on s.code=<span style="color: rgba(0, 0, 0, 1);">e.code </span>-> group by s.name,s.code;</span> |
三、根据要求写出SQL语句
表结构:
student(s_no,s_name,s_age,sex) 学生表
teacher(t_no,t_name) 教师表
course(c_no,c_name,t_no) 课程表
sc(s_no,c_no,score) 成绩表
基础表数据(个人铺的):根据题目需要自行再铺入数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span> * <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student; </span>+------+--------+-------+------+ | s_no | s_name | s_age | sex | +------+--------+-------+------+ | <span style="color: rgba(128, 0, 128, 1);">1001</span> | 张三 | <span style="color: rgba(128, 0, 128, 1);">23</span> | 男 | | <span style="color: rgba(128, 0, 128, 1);">1002</span> | 李四 | <span style="color: rgba(128, 0, 128, 1);">19</span> | 女 | | <span style="color: rgba(128, 0, 128, 1);">1003</span> | 马五 | <span style="color: rgba(128, 0, 128, 1);">20</span> | 男 | | <span style="color: rgba(128, 0, 128, 1);">1004</span> | 甲六 | <span style="color: rgba(128, 0, 128, 1);">17</span> | 女 | | <span style="color: rgba(128, 0, 128, 1);">1005</span> | 乙七 | <span style="color: rgba(128, 0, 128, 1);">22</span> | 男 | +------+--------+-------+------+ <span style="color: rgba(128, 0, 128, 1);">5</span> rows <span style="color: rgba(0, 0, 255, 1);">in</span> <span style="color: rgba(0, 0, 255, 1);">set</span> (<span style="color: rgba(128, 0, 128, 1);">0.00</span><span style="color: rgba(0, 0, 0, 1);"> sec) mysql</span>> <span style="color: rgba(0, 0, 255, 1);">select</span> * <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> teacher; </span>+------+--------+ | t_no | t_name | +------+--------+ | <span style="color: rgba(128, 0, 128, 1);">2001</span> | 叶平 | | <span style="color: rgba(128, 0, 128, 1);">2002</span> | 赵安 | | <span style="color: rgba(128, 0, 128, 1);">2003</span> | 孙顺 | | <span style="color: rgba(128, 0, 128, 1);">2004</span> | 刘六 | +------+--------+ <span style="color: rgba(128, 0, 128, 1);">4</span> rows <span style="color: rgba(0, 0, 255, 1);">in</span> <span style="color: rgba(0, 0, 255, 1);">set</span> (<span style="color: rgba(128, 0, 128, 1);">0.00</span><span style="color: rgba(0, 0, 0, 1);"> sec) mysql</span>> <span style="color: rgba(0, 0, 255, 1);">select</span> * <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> course; </span>+------+--------------+------+ | c_no | c_name | t_no | +------+--------------+------+ | <span style="color: rgba(128, 0, 128, 1);">001</span> | 企业管理 | <span style="color: rgba(128, 0, 128, 1);">2001</span> | | <span style="color: rgba(128, 0, 128, 1);">002</span> | 马克思 | <span style="color: rgba(128, 0, 128, 1);">2002</span> | | <span style="color: rgba(128, 0, 128, 1);">003</span> | UML | <span style="color: rgba(128, 0, 128, 1);">2003</span> | | <span style="color: rgba(128, 0, 128, 1);">004</span> | 数据库 | <span style="color: rgba(128, 0, 128, 1);">2004</span> | +------+--------------+------+ <span style="color: rgba(128, 0, 128, 1);">4</span> rows <span style="color: rgba(0, 0, 255, 1);">in</span> <span style="color: rgba(0, 0, 255, 1);">set</span> (<span style="color: rgba(128, 0, 128, 1);">0.05</span><span style="color: rgba(0, 0, 0, 1);"> sec) mysql</span>> <span style="color: rgba(0, 0, 255, 1);">select</span> * <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> sc; </span>+------+------+-------+ | s_no | c_no | score | +------+------+-------+ | <span style="color: rgba(128, 0, 128, 1);">1001</span> | <span style="color: rgba(128, 0, 128, 1);">001</span> | <span style="color: rgba(128, 0, 128, 1);">93</span> | | <span style="color: rgba(128, 0, 128, 1);">1001</span> | <span style="color: rgba(128, 0, 128, 1);">002</span> | <span style="color: rgba(128, 0, 128, 1);">86</span> | | <span style="color: rgba(128, 0, 128, 1);">1001</span> | <span style="color: rgba(128, 0, 128, 1);">004</span> | <span style="color: rgba(128, 0, 128, 1);">92</span> | | <span style="color: rgba(128, 0, 128, 1);">1002</span> | <span style="color: rgba(128, 0, 128, 1);">003</span> | <span style="color: rgba(128, 0, 128, 1);">100</span> | | <span style="color: rgba(128, 0, 128, 1);">1003</span> | <span style="color: rgba(128, 0, 128, 1);">001</span> | <span style="color: rgba(128, 0, 128, 1);">93</span> | | <span style="color: rgba(128, 0, 128, 1);">1003</span> | <span style="color: rgba(128, 0, 128, 1);">004</span> | <span style="color: rgba(128, 0, 128, 1);">99</span> | | <span style="color: rgba(128, 0, 128, 1);">1004</span> | <span style="color: rgba(128, 0, 128, 1);">002</span> | <span style="color: rgba(128, 0, 128, 1);">75</span> | | <span style="color: rgba(128, 0, 128, 1);">1004</span> | <span style="color: rgba(128, 0, 128, 1);">003</span> | <span style="color: rgba(128, 0, 128, 1);">59</span> | | <span style="color: rgba(128, 0, 128, 1);">1002</span> | <span style="color: rgba(128, 0, 128, 1);">002</span> | <span style="color: rgba(128, 0, 128, 1);">50</span> | | <span style="color: rgba(128, 0, 128, 1);">1005</span> | <span style="color: rgba(128, 0, 128, 1);">003</span> | <span style="color: rgba(128, 0, 128, 1);">60</span> | | <span style="color: rgba(128, 0, 128, 1);">1005</span> | <span style="color: rgba(128, 0, 128, 1);">002</span> | <span style="color: rgba(128, 0, 128, 1);">60</span> | +------+------+-------+ <span style="color: rgba(128, 0, 128, 1);">11</span> rows <span style="color: rgba(0, 0, 255, 1);">in</span> <span style="color: rgba(0, 0, 255, 1);">set</span> (<span style="color: rgba(128, 0, 128, 1);">0.00</span> sec)</span> |
1、查询“001”课程比“002”课程成绩高的所有学生的学号。
1 2 3 4 5 6 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> a.s_no </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span> -> (<span style="color: rgba(0, 0, 255, 1);">select</span> s_no,score <span style="color: rgba(0, 0, 255, 1);">from</span> sc <span style="color: rgba(0, 0, 255, 1);">where</span> c_no=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">001</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">) a, </span>-> (<span style="color: rgba(0, 0, 255, 1);">select</span> s_no,score <span style="color: rgba(0, 0, 255, 1);">from</span> sc <span style="color: rgba(0, 0, 255, 1);">where</span> c_no=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">002</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">) b </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> a.score><span style="color: rgba(0, 0, 0, 1);">b.score </span>-> and a.s_no=b.s_no;</span> |
2、查询平均成绩大于60分的同学的学号和平均成绩。
1 2 3 4 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> s_no,avg(score) </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> sc </span>-><span style="color: rgba(0, 0, 0, 1);"> group by s_no </span>-> having avg(score)><span style="color: rgba(128, 0, 128, 1);">60</span>;</span> |
3、查询所有同学的学号、姓名、选课数、总成绩。
1 2 3 4 5 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> student.s_no,student.s_name,count(sc.c_no),sum(sc.score) </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student </span>-><span style="color: rgba(0, 0, 0, 1);"> left join sc </span>-> on student.s_no=<span style="color: rgba(0, 0, 0, 1);">sc.s_no </span>-> group by student.s_no,student.s_name;</span> |
4、查询姓李的老师的个数。
1 2 3 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span> count(*<span style="color: rgba(0, 0, 0, 1);">) </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> teacher </span> -> <span style="color: rgba(0, 0, 255, 1);">where</span> t_name like <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">李%</span><span style="color: rgba(128, 0, 0, 1);">'</span>;</span> |
5、查询没学过“叶平”老师课的同学的学号、姓名
1 2 3 4 5 6 7 8 9 10 11 12 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> student.s_no,student.s_name </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> student.s_no not <span style="color: rgba(0, 0, 255, 1);">in</span> -><span style="color: rgba(0, 0, 0, 1);"> ( </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> distinct(sc.s_no) </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> sc </span>-><span style="color: rgba(0, 0, 0, 1);"> join course </span>-> on course.c_no=<span style="color: rgba(0, 0, 0, 1);">sc.c_no </span>-><span style="color: rgba(0, 0, 0, 1);"> join teacher </span>-> on teacher.t_no=<span style="color: rgba(0, 0, 0, 1);">course.t_no </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> t_name=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">叶平</span><span style="color: rgba(128, 0, 0, 1);">'</span> -> );</span> |
6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名。
1 2 3 4 5 6 7 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> student.s_no,student.s_name </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student </span>-><span style="color: rgba(0, 0, 0, 1);"> join sc </span>-> on sc.s_no=<span style="color: rgba(0, 0, 0, 1);">student.s_no </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> c_no=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">001</span><span style="color: rgba(128, 0, 0, 1);">'</span> -><span style="color: rgba(0, 0, 0, 1);"> and exists </span>-> (<span style="color: rgba(0, 0, 255, 1);">select</span> * <span style="color: rgba(0, 0, 255, 1);">from</span> sc <span style="color: rgba(0, 0, 255, 1);">where</span> sc.s_no=student.s_no and c_no=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">002</span><span style="color: rgba(128, 0, 0, 1);">'</span>);</span> |
7、查询学过“叶平”老师所教的所有课的同学的学号、姓名。
1 2 3 4 5 6 7 8 9 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> student.s_no,student.s_name </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student </span>-><span style="color: rgba(0, 0, 0, 1);"> join sc </span>-> on sc.s_no=<span style="color: rgba(0, 0, 0, 1);">student.s_no </span>-><span style="color: rgba(0, 0, 0, 1);"> join course </span>-> on course.c_no=<span style="color: rgba(0, 0, 0, 1);">sc.c_no </span>-><span style="color: rgba(0, 0, 0, 1);"> join teacher </span>-> on teacher.t_no=<span style="color: rgba(0, 0, 0, 1);">course.t_no </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> teacher.t_name=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">叶平</span><span style="color: rgba(128, 0, 0, 1);">'</span>;</span> |
8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名。
1 2 3 4 5 6 7 8 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> student.s_no,student.s_name </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student </span>-> join (<span style="color: rgba(0, 0, 255, 1);">select</span> s_no,score <span style="color: rgba(0, 0, 255, 1);">from</span> sc <span style="color: rgba(0, 0, 255, 1);">where</span> c_no=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">001</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">) a </span>-> on a.s_no=<span style="color: rgba(0, 0, 0, 1);">student.s_no </span>-> join (<span style="color: rgba(0, 0, 255, 1);">select</span> s_no,score <span style="color: rgba(0, 0, 255, 1);">from</span> sc <span style="color: rgba(0, 0, 255, 1);">where</span> c_no=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">002</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">) b </span>-> on b.s_no=<span style="color: rgba(0, 0, 0, 1);">student.s_no </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> a.s_no=<span style="color: rgba(0, 0, 0, 1);">b.s_no </span>-> and a.score>b.score;</span> |
9、查询所有课程成绩小于60分的同学的学号、姓名。
1 2 3 4 5 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> student.s_no,student.s_name </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student </span>-><span style="color: rgba(0, 0, 0, 1);"> join sc </span>-> on sc.s_no=<span style="color: rgba(0, 0, 0, 1);">student.s_no </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> sc.score<<span style="color: rgba(128, 0, 128, 1);">60</span>;</span> |
10、查询没有学全所有课的同学的学号、姓名。
1 2 3 4 5 6 7 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> student.s_no 学号,student.s_name 姓名 </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student </span>-><span style="color: rgba(0, 0, 0, 1);"> left join sc </span>-> on sc.s_no=<span style="color: rgba(0, 0, 0, 1);">student.s_no </span>-><span style="color: rgba(0, 0, 0, 1);"> group by student.s_no,student.s_name </span>-> having count(*) <<span style="color: rgba(0, 0, 0, 1);"> ( </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span> count(*) <span style="color: rgba(0, 0, 255, 1);">from</span> course);</span> |
11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名。
1 2 3 4 5 6 7 8 9 10 11 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> student.s_no,student.s_name </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student </span>-><span style="color: rgba(0, 0, 0, 1);"> join sc </span>-> on sc.s_no=<span style="color: rgba(0, 0, 0, 1);">student.s_no </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> sc.c_no <span style="color: rgba(0, 0, 255, 1);">in</span> -><span style="color: rgba(0, 0, 0, 1);"> ( </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> c_no </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> sc </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> s_no=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">1001</span><span style="color: rgba(128, 0, 0, 1);">'</span> -><span style="color: rgba(0, 0, 0, 1);"> ) </span>-> and student.s_no != <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">1001</span><span style="color: rgba(128, 0, 0, 1);">'</span>;</span> |
12、查询至少学过学号为“1001”同学所有一门课的其他同学学号和姓名。
1 2 3 4 5 6 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> distinct sc.s_no,s_name </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student,sc </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> student.s_no=<span style="color: rgba(0, 0, 0, 1);">sc.s_no </span>-> and c_no <span style="color: rgba(0, 0, 255, 1);">in</span> -> (<span style="color: rgba(0, 0, 255, 1);">select</span> c_no <span style="color: rgba(0, 0, 255, 1);">from</span> sc <span style="color: rgba(0, 0, 255, 1);">where</span> s_no=<span style="color: rgba(128, 0, 128, 1);">1001</span><span style="color: rgba(0, 0, 0, 1);">) </span>-> and student.s_no != <span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">1001</span><span style="color: rgba(128, 0, 0, 1);">'</span>;</span> |
13、把“sc”表中“叶平”老师叫的课的成绩都更改为此课程的平均成绩。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">set</span> @ye_avg_score= -><span style="color: rgba(0, 0, 0, 1);"> ( </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> avg(score) </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span> -><span style="color: rgba(0, 0, 0, 1);"> ( </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> sc.score </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> sc </span>-><span style="color: rgba(0, 0, 0, 1);"> join course </span>-> on course.c_no=<span style="color: rgba(0, 0, 0, 1);">sc.c_no </span>-><span style="color: rgba(0, 0, 0, 1);"> join teacher </span>-> on teacher.t_no=<span style="color: rgba(0, 0, 0, 1);">course.t_no </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> teacher.t_name=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">叶平</span><span style="color: rgba(128, 0, 0, 1);">'</span> -><span style="color: rgba(0, 0, 0, 1);"> ) azi </span> -><span style="color: rgba(0, 0, 0, 1);"> ); mysql</span>><span style="color: rgba(0, 0, 0, 1);"> update sc </span>-> <span style="color: rgba(0, 0, 255, 1);">set</span> score=<span style="color: rgba(0, 0, 0, 1);">@ye_avg_score </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> c_no <span style="color: rgba(0, 0, 255, 1);">in</span> -><span style="color: rgba(0, 0, 0, 1);"> ( </span>-> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> c_no </span>-> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> course </span>-><span style="color: rgba(0, 0, 0, 1);"> join teacher </span>-> on teacher.t_no=<span style="color: rgba(0, 0, 0, 1);">course.t_no </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> teacher.t_name=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">叶平</span><span style="color: rgba(128, 0, 0, 1);">'</span> -> );</span> |
14、查询和“1002”号同学学习的课程完全相同的其他同学学号和姓名。
1 2 3 4 5 6 7 8 9 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span><span style="color: rgba(0, 0, 0, 1);"> s_no,s_name </span> -> <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> student </span> -> <span style="color: rgba(0, 0, 255, 1);">where</span> s_no <span style="color: rgba(0, 0, 255, 1);">in</span><span style="color: rgba(0, 0, 0, 1);"> ( </span> -> <span style="color: rgba(0, 0, 255, 1);">select</span> distinct s_no <span style="color: rgba(0, 0, 255, 1);">from</span> sc <span style="color: rgba(0, 0, 255, 1);">where</span> c_no <span style="color: rgba(0, 0, 255, 1);">in</span> -> (<span style="color: rgba(0, 0, 255, 1);">select</span> c_no <span style="color: rgba(0, 0, 255, 1);">from</span> sc <span style="color: rgba(0, 0, 255, 1);">where</span> s_no=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">1002</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">) </span> -><span style="color: rgba(0, 0, 0, 1);"> group by s_no </span> -> having count(*)=(<span style="color: rgba(0, 0, 255, 1);">select</span> count(*) <span style="color: rgba(0, 0, 255, 1);">from</span> sc <span style="color: rgba(0, 0, 255, 1);">where</span> s_no=<span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">1002</span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(0, 0, 0, 1);">) </span> -> and s_no<><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">1002</span><span style="color: rgba(128, 0, 0, 1);">'</span> -> );</span> |
15、删除学习“叶平”老师课的sc表记录。
1 2 3 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">set</span> @ye_c_no=(<span style="color: rgba(0, 0, 255, 1);">select</span> c_no <span style="color: rgba(0, 0, 255, 1);">from</span> course,teacher <span style="color: rgba(0, 0, 255, 1);">where</span> course.t_no=teacher.t_no and t_name=<span style="color: rgba(0, 0, 0, 1);">’叶平’); mysql</span>> delete <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> sc </span>-> <span style="color: rgba(0, 0, 255, 1);">where</span> c_no=@ye_c_no;</span> |
16、向sc表中插入一些记录,这些记录要求符合一下条件:没有上过编号“003”课程的同学学号
1 2 3 |
<span style="font-size: 16px;">mysql> <span style="color: rgba(0, 0, 255, 1);">select</span> distinct s_no <span style="color: rgba(0, 0, 255, 1);">from</span><span style="color: rgba(0, 0, 0, 1);"> sc & </span></span> |