1. 程式人生 > >Mysql連表查詢習題

Mysql連表查詢習題

練習題

表一

mysql> create database linux50 charset utf8;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| linux50            |
| ming               |
| mysql              |
| performance_schema |
| test               |
| world              |
| xudao              |
+--------------------+
8 rows in set (0.00 sec)
mysql> \u linux50
Database changed
mysql> create table student(sno bigint(20) not null primary key auto_increment comment '學號',
    -> sname varchar(300) not null comment '學生姓名',
    -> sage tinyint unsigned not null comment '學生年齡',
    -> ssex enum('1','0') not null  default '1' comment '學生性別',
    -> sbirthday datetime  default null comment '學生生日',
    -> class int not null comment '學生班級') ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.01 sec)

mysql> select *from student;
+-----+-----------+------+------+---------------------+-------+
| sno | sname     | sage | ssex | sbirthday           | class |
+-----+-----------+------+------+---------------------+-------+
|   1 | 王麗英    |   18 | 1    | 2018-11-28 17:45:58 |     7 |
|   2 | 王亞嬌    |   19 | 0    | 2018-11-28 17:50:07 |     7 |
|   3 | 程康華    |   22 | 1    | 2018-11-28 17:50:40 |     7 |
|   4 | 郭亞望    |   20 | 1    | 2018-11-28 17:51:19 |     7 |
|   5 | 文長清    |   21 | 1    | 2018-11-28 17:51:42 |     7 |
|   6 | 馬慧芬    |   20 | 1    | 2018-11-28 20:54:04 |     5 |
|   7 | 王晶      |   20 | 0    | 2018-11-28 20:55:00 |     3 |
+-----+-----------+------+------+---------------------+-------+

Mysql連表查詢習題

表二

mysql> create table source(cno bigint(20) not null primary key auto_increment comment '課程號',  cname varchar(50) not null comment '課程名稱',  tno int(3) zerofill not null  comment '教師編號'  );
Query OK, 0 rows affected (0.02 sec)
mysql> select *from source;
+-----+--------+-----+
| cno | cname  | tno |
+-----+--------+-----+
|   1 | 語文   | 001 |
|   2 | 數學   | 002 |
|   3 | 英語   | 003 |
+-----+--------+-----+

Mysql連表查詢習題

表三

mysql> create table score(sno bigint(20) not null comment '學號',
    -> cno bigint(20) not null  comment '課程號',
    -> mark double(4,1) not null comment '成績',
    -> primary key(sno,cno)
    -> );
   mysql> select *from score;
+-----+-----+-------+
| sno | cno | mark  |
+-----+-----+-------+
|   1 |   1 |  90.0 |
|   2 |   1 |  90.0 |
|   2 |   2 |  70.0 |
|   2 |   3 |  70.0 |
|   3 |   1 |  95.0 |
|   3 |   2 | 100.0 |
+-----+-----+-------+
6 rows in set (0.00 sec)

Mysql連表查詢習題

表四

create table teacher(cno int(3) zerofill not null primary key auto_increment comment '教師編號',
tname varchar(50) not null comment '教師姓名',
tage tinyint unsigned not null comment '教師年齡',
tsex enum('1','0') not null default '1' comment '教師性別',
prof varchar(100) comment '教師職稱',
depart varchar(50) comment '教師部門'
);

Mysql連表查詢習題

集合練習

查詢練習:

1.查詢student表中的所有記錄的sname、ssex和class列。

mysql> select sname,ssex,class from student;
+-----------+------+-------+
| sname     | ssex | class |
+-----------+------+-------+
| 王麗英    | 1    |     7 |
| 王亞嬌    | 0    |     7 |
| 程康華    | 1    |     7 |
| 郭亞望    | 1    |     7 |
| 文長清    | 1    |     7 |
| 馬慧芬    | 1    |     5 |
| 王晶      | 0    |     3 |
+-----------+------+-------+

2.查詢教師所有的單位即不重複的depart列。

mysql> select *from teacher;
+-----+--------------+------+------+--------------+--------------+
| cno | tname        | tage | tsex | prof         | depart       |
+-----+--------------+------+------+--------------+--------------+
| 001 | 增志高翔     |   23 | 1    | DBA老大      | DBA系        |
| 002 | 徐亮偉       |   24 | 1    | 講師老大     | Linux系      |
| 003 | 李泳誼       |   26 | 1    | 綜合老大     | 老男孩系     |
| 004 | 老男孩       |   24 | 1    | 公司老大     | 老男孩系     |
+-----+--------------+------+------+--------------+--------------+

Mysql連表查詢習題
Mysql連表查詢習題

3.查詢student表的所有記錄。

mysql> select *from student;
+-----+-----------+------+------+---------------------+-------+
| sno | sname     | sage | ssex | sbirthday           | class |
+-----+-----------+------+------+---------------------+-------+
|   1 | 王麗英    |   18 | 1    | 2018-11-28 17:45:58 |     7 |
|   2 | 王亞嬌    |   19 | 0    | 2018-11-28 17:50:07 |     7 |
|   3 | 程康華    |   22 | 1    | 2018-11-28 17:50:40 |     7 |
|   4 | 郭亞望    |   20 | 1    | 2018-11-28 17:51:19 |     7 |
|   5 | 文長清    |   21 | 1    | 2018-11-28 17:51:42 |     7 |
|   6 | 馬慧芬    |   20 | 1    | 2018-11-28 20:54:04 |     5 |
|   7 | 王晶      |   20 | 0    | 2018-11-28 20:55:00 |     3 |
+-----+-----------+------+------+---------------------+-------+

4.查詢score表中成績在60到80之間的所有記錄。

Mysql連表查詢習題
Mysql連表查詢習題

5.查詢score表中成績為85,86或88的記錄。

select * from score where Degree in (90,95,70);

6.查詢student表中7班或性別為“女”的同學記錄。

Mysql連表查詢習題

7.以class降序查詢Student表的所有記錄。

mysql> select *from student order by class desc;
+-----+-----------+------+------+---------------------+-------+
| sno | sname     | sage | ssex | sbirthday           | class |
+-----+-----------+------+------+---------------------+-------+
|   1 | 王麗英    |   18 | 1    | 2018-11-28 17:45:58 |     7 |
|   2 | 王亞嬌    |   19 | 0    | 2018-11-28 17:50:07 |     7 |
|   3 | 程康華    |   22 | 1    | 2018-11-28 17:50:40 |     7 |
|   4 | 郭亞望    |   20 | 1    | 2018-11-28 17:51:19 |     7 |
|   5 | 文長清    |   21 | 1    | 2018-11-28 17:51:42 |     7 |
|   6 | 馬慧芬    |   20 | 1    | 2018-11-28 20:54:04 |     5 |
|   7 | 王晶      |   20 | 0    | 2018-11-28 20:55:00 |     3 |

8.以cno升序、mark降序查詢Score表的所有記錄

mysql> select *from score order by cno;
+-----+-----+-------+
| sno | cno | mark  |
+-----+-----+-------+
|   1 |   1 |  90.0 |
|   2 |   1 |  90.0 |
|   3 |   1 |  95.0 |
|   2 |   2 |  70.0 |
|   3 |   2 | 100.0 |
|   2 |   3 |  70.0 |
+-----+-----+-------+
mysql> select *from score order by  mark desc;
+-----+-----+-------+
| sno | cno | mark  |
+-----+-----+-------+
|   3 |   2 | 100.0 |
|   3 |   1 |  95.0 |
|   1 |   1 |  90.0 |
|   2 |   1 |  90.0 |
|   2 |   2 |  70.0 |
|   2 |   3 |  70.0 |
+-----+-----+-------+

9.查詢7班的學生人數。

mysql> select count(*)  from student where class='7';
+----------+
| count(*) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

10.查詢”曾志高翔“教師任課的學生成績。

mysql> select teacher.tname, student.sno,student.sname,score.mark 
    -> from teacher,student,score,course
    -> where student.sno=score.sno and
    -> score.cno=course.cno
    -> and course.tno=teacher.tno 
    -> and teacher.tno='001';

11.查詢語文課程所有男生的成績並且查出對應課程的教師名,職稱,及所在部門。

mysql> select student.sname,score.mark,teacher.tname,teacher.prof,teacher.depart 
    -> from teacher,student,score,course
    -> where student.sno=score.sno and
    -> score.cno=course.cno and
    -> course.tno=teacher.tno and
    -> student.ssex='1'
-> and course.cname='語文';

12.把11題查出的成績按照降序排序。

mysql> select student.sname,score.mark,teacher.tname,teacher.prof,teacher.depart 
    -> from teacher,student,score,course
    -> where student.sno=score.sno and
    -> score.cno=course.cno and
    -> course.tno=teacher.tno and
    -> student.ssex='1'
-> and course.cname='語文'
-> order by score.mark desc;