mysql学习笔记四

MySQL查询练习

  1. 准备数据表
mysql> create database work;
Query OK, 1 row affected (0.01 sec)

mysql> use work
Database changed

学生表student:(学号,姓名,出生年月,所在班级)

mysql> create table student(
    -> sno varchar(20) primary key,
    -> sname varchar(20) not null,
    -> sbirthday datetime,
    -> class varchar(20)
    -> );
Query OK, 0 rows affected (0.04 sec)

教师表teacher(教师编号,教师名字,教师性别,出生年月,职称,所在部门)

mysql> create table teacher(
    -> tno varchar(20) primary key,
    -> tname varchar(20) not null,
    -> tsex varchar(20) not null,
    -> tbirthday datetime,
    -> prof varchar(20) not null,
    -> depart varchar(20) not null);
Query OK, 0 rows affected (0.01 sec)

课程表course:(课程号,课程名称,教师编号)

mysql> create table course(
    -> cno varchar(20) primary key,
    -> cname varchar(20) not null,
    -> tno varchar(20) not null,
    -> foreign key(tno) references teacher(tno)
    -> );
Query OK, 0 rows affected (0.02 sec)

成绩表score(学号,课程号,成绩)

mysql> create table score(
    -> sno varchar(20) primary key,
    -> cno varchar(20) not null,
    -> degree decimal,
    -> foreign key(sno) references student(sno),
    -> foreign key(cno) references course(cno)
    -> );
Query OK, 0 rows affected (0.03 sec)
  1. 往数据表中添加数据
    添加学生信息
mysql> insert into student values('101','zeng','1977-09-01','95033');
Query OK, 1 row affected (0.01 sec)

mysql> insert into student values('102','ming','1975-09-01','95031');
Query OK, 1 row affected (0.00 sec)

mysql> insert into student values('103','wang','1976-09-01','95033');
Query OK, 1 row affected (0.00 sec)

mysql> insert into student values('104','li','1977-02-01','95033');
Query OK, 1 row affected (0.00 sec)

mysql> insert into student values('105','lu','1972-02-09','95031');
Query OK, 1 row affected (0.00 sec)

mysql> insert into student values('106','jun','1974-06-03','95031');
Query OK, 1 row affected (0.01 sec)

添加教师信息

mysql> insert into teacher values('804','cheng','m','1958-09-01','lecturer','computer');
Query OK, 1 row affected (0.01 sec)

mysql> insert into teacher values('805','xun','m','1969-09-01','lecturer','math');
Query OK, 1 row affected (0.00 sec)

mysql> insert into teacher values('806','ping','w','1972-09-01','professor','computer');
Query OK, 1 row affected (0.00 sec)

mysql> insert into teacher values('807','bing','w','1977-08-16','professor','math');
Query OK, 1 row affected (0.01 sec)

添加课程信息

mysql> insert into course values('3-105','computer','804');
Query OK, 1 row affected (0.01 sec)

mysql> insert into course values('4-105','math','805');
Query OK, 1 row affected (0.00 sec)

mysql> insert into course values('3-107','computer','806');
Query OK, 1 row affected (0.00 sec)

mysql> insert into course values('4-115','math','807');
Query OK, 1 row affected (0.00 sec)

添加成绩信息

mysql> insert into score values('101','3-105','86');
Query OK, 1 row affected (0.01 sec)

mysql> insert into score values('102','4-105','96');
Query OK, 1 row affected (0.00 sec)

mysql> insert into score values('103','3-105','89');
Query OK, 1 row affected (0.00 sec)

mysql> insert into score values('104','3-105','66');
Query OK, 1 row affected (0.00 sec)

mysql> insert into score values('105','3-107','81');
Query OK, 1 row affected (0.00 sec)

mysql> insert into score values('106','4-115','76');
Query OK, 1 row affected (0.01 sec)
  1. 查询练习
    3.1 查询student表中的所有记录的sname,class类
mysql> select sname,class from student;
+-------+-------+
| sname | class |
+-------+-------+
| zeng  | 95033 |
| ming  | 95031 |
| wang  | 95033 |
| li    | 95033 |
| lu    | 95031 |
| jun   | 95031 |
+-------+-------+
6 rows in set (0.00 sec)

3.2 查询教师所有的单位即不重复的depart列(distinct排除重复)

mysql> select distinct depart from teacher;
+----------+
| depart   |
+----------+
| computer |
| math     |
+----------+
2 rows in set (0.00 sec)

3.3 查询score表中成绩在60到80之间的所有记录
方法一:(查询区间 between...and...)

mysql> select * from score where degree between 60 and 80;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 104 | 3-105 |     66 |
| 106 | 4-115 |     76 |
+-----+-------+--------+
2 rows in set (0.00 sec)

方法二:(直接使用运算符比较)

mysql> select * from score where degree>60 and degree<80;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 104 | 3-105 |     66 |
| 106 | 4-115 |     76 |
+-----+-------+--------+
2 rows in set (0.08 sec)

3.4 查询score表中成绩为85,86,96的记录

mysql> select * from score where degree in(85,86,96);
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 101 | 3-105 |     86 |
| 102 | 4-105 |     96 |
+-----+-------+--------+
2 rows in set (0.00 sec

3.5 查询teacher表中"computer"系或性别为"女"的记录

mysql> select * from teacher where depart="computer" or tsex="m";
+-----+-------+------+---------------------+-----------+----------+
| tno | tname | tsex | tbirthday           | prof      | depart   |
+-----+-------+------+---------------------+-----------+----------+
| 804 | cheng | m    | 1958-09-01 00:00:00 | lecturer  | computer |
| 805 | xun   | m    | 1969-09-01 00:00:00 | lecturer  | math     |
| 806 | ping  | w    | 1972-09-01 00:00:00 | professor | computer |
+-----+-------+------+---------------------+-----------+----------+
3 rows in set (1.63 sec)

3.6 以class降序查询student表中的所有记录(asc或默认表示升序,desc表示降序)

mysql> select * from student order by class desc;
+-----+-------+---------------------+-------+
| sno | sname | sbirthday           | class |
+-----+-------+---------------------+-------+
| 101 | zeng  | 1977-09-01 00:00:00 | 95033 |
| 103 | wang  | 1976-09-01 00:00:00 | 95033 |
| 104 | li    | 1977-02-01 00:00:00 | 95033 |
| 102 | ming  | 1975-09-01 00:00:00 | 95031 |
| 105 | lu    | 1972-02-09 00:00:00 | 95031 |
| 106 | jun   | 1974-06-03 00:00:00 | 95031 |
+-----+-------+---------------------+-------+
6 rows in set (1.66 sec)

3.7 以cno升序、degree降序查询score表的所有记录

mysql> select * from score order by cno asc,degree desc;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |     89 |
| 101 | 3-105 |     86 |
| 104 | 3-105 |     66 |
| 105 | 3-107 |     81 |
| 102 | 4-105 |     96 |
| 106 | 4-115 |     76 |
+-----+-------+--------+
6 rows in set (0.00 sec)

3.8 查询"95031"班的学生人数

mysql> select count(*) from student where class="95031";
+----------+
| count(*) |
+----------+
|        3 |
+----------+
1 row in set (0.00 sec)

3.9 查询score表中的最高分的学生学号和课程号。(子查询或者排序)
子查询:

mysql> select sno,cno from score where degree=(select max(degree) from score);
+-----+-------+
| sno | cno   |
+-----+-------+
| 102 | 4-105 |
+-----+-------+
1 row in set (0.00 sec)

排序:(当最大值有多个时,使用排序需要注意,若最大值个数出现错误查询结果将不准确)
limit :第一个数字表示开始位置,第二个数字表示查询多少条

mysql> select sno,cno from score order by degree desc limit 0,1;
+-----+-------+
| sno | cno   |
+-----+-------+
| 102 | 4-105 |
+-----+-------+
1 row in set (0.00 sec)

3.10 查询每门课的平均成绩

mysql> select cno,avg(degree) from score group by cno;
+-------+-------------+
| cno   | avg(degree) |
+-------+-------------+
| 3-105 |     80.3333 |
| 3-107 |     81.0000 |
| 4-105 |     96.0000 |
| 4-115 |     76.0000 |
+-------+-------------+
4 rows in set (0.00 sec)

3.11 查询score表中至少有2名学生选修的并以3开头的课程的平均分数(group by后面跟条件需要用having)

mysql> select cno,avg(degree) from score group by cno having count(cno)>=2 and cno like '3%';
+-------+-------------+
| cno   | avg(degree) |
+-------+-------------+
| 3-105 |     80.3333 |
+-------+-------------+
1 row in set (0.00 sec)

3.12 查询分数大于70,小于90的sno列

mysql> select sno,degree from score where degree>70 and degree<90;
+-----+--------+
| sno | degree |
+-----+--------+
| 101 |     86 |
| 103 |     89 |
| 105 |     81 |
| 106 |     76 |
+-----+--------+
4 rows in set (0.00 sec)

mysql> select sno,degree from score where degree between 70 and 90;
+-----+--------+
| sno | degree |
+-----+--------+
| 101 |     86 |
| 103 |     89 |
| 105 |     81 |
| 106 |     76 |
+-----+--------+
4 rows in set (0.00 sec)

3.13 查询所有学生的sname,cno,和degree列

mysql> select sname,cno,degree from student,score where student.sno=score.sno;
+-------+-------+--------+
| sname | cno   | degree |
+-------+-------+--------+
| zeng  | 3-105 |     86 |
| ming  | 4-105 |     96 |
| wang  | 3-105 |     89 |
| li    | 3-105 |     66 |
| lu    | 3-107 |     81 |
| jun   | 4-115 |     76 |
+-------+-------+--------+
6 rows in set (0.00 sec)

3.14 查询所有学生的sno,cname和degree列

mysql> select sno,cname,degree from course,score where course.cno=score.cno;
+-----+----------+--------+
| sno | cname    | degree |
+-----+----------+--------+
| 101 | computer |     86 |
| 103 | computer |     89 |
| 104 | computer |     66 |
| 105 | computer |     81 |
| 102 | math     |     96 |
| 106 | math     |     76 |
+-----+----------+--------+
6 rows in set (0.00 sec)

3.15 查询所有学生的sname,cname和degree列

mysql> select sname,cname,degree from student,course,score where student.sno=score.sno and course.cno=score.cno;
+-------+----------+--------+
| sname | cname    | degree |
+-------+----------+--------+
| zeng  | computer |     86 |
| wang  | computer |     89 |
| li    | computer |     66 |
| lu    | computer |     81 |
| ming  | math     |     96 |
| jun   | math     |     76 |
+-------+----------+--------+
6 rows in set (0.00 sec)

3.16 查询"95031"班学生每门课的平均分

mysql> select cno,avg(degree) from score where sno in (select sno from student where class = "95031")
    -> group by cno;
+-------+-------------+
| cno   | avg(degree) |
+-------+-------------+
| 3-107 |     81.0000 |
| 4-105 |     96.0000 |
| 4-115 |     76.0000 |
+-------+-------------+
3 rows in set (0.00 sec)

3.17 查询选修"3-105"课程的成绩高于"104"号同学的所有同学的记录

mysql> select * from score where cno='3-105' and degree>(select degree from score where sno='104' and cno='3-105');
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 101 | 3-105 |     86 |
| 103 | 3-105 |     89 |
+-----+-------+--------+
2 rows in set (0.00 sec)

3.18 查询成绩高于学号为"104",课程号为"3-105"的成绩的所有记录

mysql> select * from score where degree>(select degree from score where sno='104' and cno='3-105');
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 101 | 3-105 |     86 |
| 102 | 4-105 |     96 |
| 103 | 3-105 |     89 |
| 105 | 3-107 |     81 |
| 106 | 4-115 |     76 |
+-----+-------+--------+
5 rows in set (1.74 sec)

3.19 查询和学号104,101同学同年出生的所有学生的sno,sname和sbirthday列

mysql> select sno,sname,sbirthday from student where year(sbirthday) in (select year(sbirthday) from student where sno in (104,101));
+-----+-------+---------------------+
| sno | sname | sbirthday           |
+-----+-------+---------------------+
| 101 | zeng  | 1977-09-01 00:00:00 |
| 104 | li    | 1977-02-01 00:00:00 |
+-----+-------+---------------------+
2 rows in set (0.00 sec)

3.20 查询"ping"教师任课的学生成绩

mysql> select degree from score where cno=(select cno from course where tno=(select tno from teacher where tname='ping'));
+--------+
| degree |
+--------+
|     81 |
+--------+
1 row in set (0.00 sec)

3.21 查询选修某课程的同学人数多于2人的教师姓名

mysql> select tname from teacher where tno=(select tno from course where cno=(select cno from score group by cno having count(*)>2));
+-------+
| tname |
+-------+
| cheng |
+-------+
1 row in set (0.00 sec)

3.22 查询"95033"班和"95031"班全体学生的记录

mysql> select * from student where class in ('95033','95031');
+-----+-------+---------------------+-------+
| sno | sname | sbirthday           | class |
+-----+-------+---------------------+-------+
| 101 | zeng  | 1977-09-01 00:00:00 | 95033 |
| 102 | ming  | 1975-09-01 00:00:00 | 95031 |
| 103 | wang  | 1976-09-01 00:00:00 | 95033 |
| 104 | li    | 1977-02-01 00:00:00 | 95033 |
| 105 | lu    | 1972-02-09 00:00:00 | 95031 |
| 106 | jun   | 1974-06-03 00:00:00 | 95031 |
+-----+-------+---------------------+-------+
6 rows in set (0.00 sec)

3.23 查询成绩在85分以上的课程cno

mysql> select cno,degree from score where degree>85;
+-------+--------+
| cno   | degree |
+-------+--------+
| 3-105 |     86 |
| 4-105 |     96 |
| 3-105 |     89 |
+-------+--------+
3 rows in set (0.00 sec)

3.24 查询出"computer"部门的教师所教课程的成绩表

mysql> select degree,cno from score where cno in (select cno from course where tno in (select tno from teacher where depart='computer'));
+--------+-------+
| degree | cno   |
+--------+-------+
|     86 | 3-105 |
|     89 | 3-105 |
|     66 | 3-105 |
|     81 | 3-107 |
+--------+-------+
4 rows in set (0.00 sec)

3.25 查询出"computer"与"math"中独特职称(是指对方没有的职称)的tname和prof(union求并集)

mysql> select tname,prof from teacher where depart='math' and prof not in(select prof from teacher where depart='computer') union
    -> select tname,prof from teacher where depart='computer' and prof not in(select prof from teacher where depart='math');
Empty set (0.37 sec)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,884评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,755评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,369评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,799评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,910评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,096评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,159评论 3 411
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,917评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,360评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,673评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,814评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,509评论 4 334
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,156评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,882评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,123评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,641评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,728评论 2 351