5.1建表与数据准备

mysql多表查询count mysql多表查询实验总结_mysql

mysql多表查询count mysql多表查询实验总结_子查询_02

1 #建表
 2 create table department(
 3 id int,
 4 name varchar(20) 
 5 );
 6 
 7 create table employee(
 8 id int primary key auto_increment,
 9 name varchar(20),
10 sex enum('male','female') not null default 'male',
11 age int,
12 dep_id int
13 );
14 
15 #插入数据
16 insert into department values
17 (200,'技术'),
18 (201,'人力资源'),
19 (202,'销售'),
20 (203,'运营');
21 
22 insert into employee(name,sex,age,dep_id) values
23 ('egon','male',18,200),
24 ('alex','female',48,201),
25 ('wupeiqi','male',38,201),
26 ('yuanhao','female',28,202),
27 ('liwenzhou','male',18,200),
28 ('jingliyang','female',18,204)
29 ;
30 
31 
32 #查看表结构和数据
33 mysql> desc department;
34 +-------+-------------+------+-----+---------+-------+
35 | Field | Type        | Null | Key | Default | Extra |
36 +-------+-------------+------+-----+---------+-------+
37 | id    | int(11)     | YES  |     | NULL    |       |
38 | name  | varchar(20) | YES  |     | NULL    |       |
39 +-------+-------------+------+-----+---------+-------+
40 
41 
42 mysql> desc employee;
43 +--------+-----------------------+------+-----+---------+----------------+
44 | Field  | Type                  | Null | Key | Default | Extra          |
45 +--------+-----------------------+------+-----+---------+----------------+
46 | id     | int(11)               | NO   | PRI | NULL    | auto_increment |
47 | name   | varchar(20)           | YES  |     | NULL    |                |
48 | sex    | enum('male','female') | NO   |     | male    |                |
49 | age    | int(11)               | YES  |     | NULL    |                |
50 | dep_id | int(11)               | YES  |     | NULL    |                |
51 +--------+-----------------------+------+-----+---------+----------------+
52 5 rows in set (0.01 sec)
53 
54 
55 mysql> select * from department;
56 +------+--------------+
57 | id   | name         |
58 +------+--------------+
59 |  200 | 技术         |
60 |  201 | 人力资源     |
61 |  202 | 销售         |
62 |  203 | 运营         |
63 +------+--------------+
64 
65 mysql> select * from employee;
66 +----+------------+--------+------+--------+
67 | id | name       | sex    | age  | dep_id |
68 +----+------------+--------+------+--------+
69 |  1 | egon       | male   |   18 |    200 |
70 |  2 | alex       | female |   48 |    201 |
71 |  3 | wupeiqi    | male   |   38 |    201 |
72 |  4 | yuanhao    | female |   28 |    202 |
73 |  5 | liwenzhou  | male   |   18 |    200 |
74 |  6 | jingliyang | female |   18 |    204 |
75 +----+------------+--------+------+--------+
76 6 rows in set (0.00 sec)

View Code

5.2多表连接查询

5.2.1交叉连接

  • 特点:不适用任何匹配条件。生成笛卡尔积
1 mysql> select * from employee,department;
 2 +----+------------+--------+------+--------+------+--------------+
 3 | id | name       | sex    | age  | dep_id | id   | name         |
 4 +----+------------+--------+------+--------+------+--------------+
 5 |  1 | egon       | male   |   18 |    200 |  200 | 技术         |
 6 |  1 | egon       | male   |   18 |    200 |  201 | 人力资源     |
 7 |  1 | egon       | male   |   18 |    200 |  202 | 销售         |
 8 |  1 | egon       | male   |   18 |    200 |  203 | 运营         |
 9 |  2 | alex       | female |   48 |    201 |  200 | 技术         |
10 |  2 | alex       | female |   48 |    201 |  201 | 人力资源     |
11 |  2 | alex       | female |   48 |    201 |  202 | 销售         |
12 |  2 | alex       | female |   48 |    201 |  203 | 运营         |
13 |  3 | wupeiqi    | male   |   38 |    201 |  200 | 技术         |
14 |  3 | wupeiqi    | male   |   38 |    201 |  201 | 人力资源     |
15 |  3 | wupeiqi    | male   |   38 |    201 |  202 | 销售         |
16 |  3 | wupeiqi    | male   |   38 |    201 |  203 | 运营         |
17 |  4 | yuanhao    | female |   28 |    202 |  200 | 技术         |
18 |  4 | yuanhao    | female |   28 |    202 |  201 | 人力资源     |
19 |  4 | yuanhao    | female |   28 |    202 |  202 | 销售         |
20 |  4 | yuanhao    | female |   28 |    202 |  203 | 运营         |
21 |  5 | liwenzhou  | male   |   18 |    200 |  200 | 技术         |
22 |  5 | liwenzhou  | male   |   18 |    200 |  201 | 人力资源     |
23 |  5 | liwenzhou  | male   |   18 |    200 |  202 | 销售         |
24 |  5 | liwenzhou  | male   |   18 |    200 |  203 | 运营         |
25 |  6 | jingliyang | female |   18 |    204 |  200 | 技术         |
26 |  6 | jingliyang | female |   18 |    204 |  201 | 人力资源     |
27 |  6 | jingliyang | female |   18 |    204 |  202 | 销售         |
28 |  6 | jingliyang | female |   18 |    204 |  203 | 运营         |
29 +----+------------+--------+------+--------+------+--------------+
  • View Code

5.2.2内连接

  • 特点:只连接匹配的行
1 #找两张表共有的部分,相当于利用条件从笛卡尔积结果中筛选出了正确的结果
 2 #department没有204这个部门,因而employee表中关于204这条员工信息没有匹配出来
 3 mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee inner join department on employee.dep_id=department.id;
 4 +----+-----------+------+--------+--------------+
 5 | id | name      | age  | sex    | name         |
 6 +----+-----------+------+--------+--------------+
 7 |  1 | egon      |   18 | male   | 技术         |
 8 |  2 | alex      |   48 | female | 人力资源     |
 9 |  3 | wupeiqi   |   38 | male   | 人力资源     |
10 |  4 | yuanhao   |   28 | female | 销售         |
11 |  5 | liwenzhou |   18 | male   | 技术         |
12 +----+-----------+------+--------+--------------+
13 
14 #上述sql等同于
15 mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee,department where employee.dep_id=department.id;
  • View Code

mysql多表查询count mysql多表查询实验总结_mysql_03

5.2.3外链接之左连接

  • 特点 :优先显示左表全部记录
1 #以左表为准,即找出所有员工信息,当然包括没有部门的员工
 2 #本质就是:在内连接的基础上增加左边有右边没有的结果
 3 mysql> select employee.id,employee.name,department.name as depart_name from employee left join department on employee.dep_id=department.id;
 4 +----+------------+--------------+
 5 | id | name       | depart_name  |
 6 +----+------------+--------------+
 7 |  1 | egon       | 技术         |
 8 |  5 | liwenzhou  | 技术         |
 9 |  2 | alex       | 人力资源     |
10 |  3 | wupeiqi    | 人力资源     |
11 |  4 | yuanhao    | 销售         |
12 |  6 | jingliyang | NULL         |
13 +----+------------+--------------+
  • View Code

mysql多表查询count mysql多表查询实验总结_查询语句_04

5.2.4 外链接之右连接

  • 特点 :优先显示右表全部记录
1 #以右表为准,即找出所有部门信息,包括没有员工的部门
 2 #本质就是:在内连接的基础上增加右边有左边没有的结果
 3 mysql> select employee.id,employee.name,department.name as depart_name from employee right join department on employee.dep_id=department.id;
 4 +------+-----------+--------------+
 5 | id   | name      | depart_name  |
 6 +------+-----------+--------------+
 7 |    1 | egon      | 技术         |
 8 |    2 | alex      | 人力资源     |
 9 |    3 | wupeiqi   | 人力资源     |
10 |    4 | yuanhao   | 销售         |
11 |    5 | liwenzhou | 技术         |
12 | NULL | NULL      | 运营         |
13 +------+-----------+--------------+
  • View Code

mysql多表查询count mysql多表查询实验总结_mysql_05

5.2.5全外连接

  • 特点 :显示左右两个表全部记录
1 全外连接:在内连接的基础上增加左边有右边没有的和右边有左边没有的结果
 2 #注意:mysql不支持全外连接 full JOIN
 3 #强调:mysql可以使用此种方式间接实现全外连接
 4 select * from employee left join department on employee.dep_id = department.id
 5 union
 6 select * from employee right join department on employee.dep_id = department.id
 7 ;
 8 #查看结果
 9 +------+------------+--------+------+--------+------+--------------+
10 | id   | name       | sex    | age  | dep_id | id   | name         |
11 +------+------------+--------+------+--------+------+--------------+
12 |    1 | egon       | male   |   18 |    200 |  200 | 技术         |
13 |    5 | liwenzhou  | male   |   18 |    200 |  200 | 技术         |
14 |    2 | alex       | female |   48 |    201 |  201 | 人力资源     |
15 |    3 | wupeiqi    | male   |   38 |    201 |  201 | 人力资源     |
16 |    4 | yuanhao    | female |   28 |    202 |  202 | 销售         |
17 |    6 | jingliyang | female |   18 |    204 | NULL | NULL        |
18 | NULL | NULL       | NULL   | NULL |   NULL |  203 | 运营         |
19 +------+------------+--------+------+--------+------+--------------+
20 
21 #注意 union与union all的区别:union会去掉相同的纪录
  • View Code

mysql多表查询count mysql多表查询实验总结_mysql_06

5.3子查询

#1:子查询是将一个查询语句嵌套在另一个查询语句中。
#2:内层查询语句的查询结果,可以为外层查询语句提供查询条件。
#3:子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
#4:还可以包含比较运算符:= 、 !=、> 、<等

5.3.1带IN关键字的子查询

#查询平均年龄在25岁以上的部门名
mysql> select id,name from department
    ->     where id in
    ->         (select dep_id from employee group by dep_id having avg(age) > 25);
+------+--------------+
| id   | name         |
+------+--------------+
|  201 | 人力资源     |
|  202 | 销售         |
+------+--------------+

#查看技术部员工姓名
mysql> select name from employee
    ->     where dep_id in
    ->         (select id from department where name='技术');
+-----------+
| name      |
+-----------+
| egon      |
| liwenzhou |
+-----------+

#查看不足1人的部门名(子查询得到的是有人的部门id)
select name from department where id not in (select distinct dep_id from employee);

5.3.2带比较运算符的子查询

  • 比较运算符:=、!=、>、>=、<、<=、<>
#查询大于所有人平均年龄的员工名与年龄
mysql> select name,age from employee where age > (select avg(age) from employee);
+---------+------+
| name    | age  |
+---------+------+
| alex    |   48 |
| wupeiqi |   38 |
+---------+------+
2 rows in set (0.00 sec)


#查询大于部门内平均年龄的员工名、年龄
mysql> select dep_id,avg(age) from employee group by dep_i
+--------+----------+
| dep_id | avg(age) |
+--------+----------+
|    200 |  18.0000 |
|    201 |  43.0000 |
|    202 |  28.0000 |
|    204 |  18.0000 |
+--------+----------+
4 rows in set (0.00 sec)

mysql> select t1.name,t1.age from employee t1
    -> inner join
    -> (select dep_id,avg(age) avg_age from employee group by dep_id) t2
    -> on t1.dep_id = t2.dep_id
    -> where t1.age > t2.avg_age;
+------+------+
| name | age  |
+------+------+
| alex |   48 |
+------+------+

5.3.3带EXISTS关键字的子查询

  • EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录。而是返回一个真假值。True或False
    当返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询`
#department表中存在dept_id=203,Ture
mysql> select * from employee
    ->     where exists
    ->         (select id from department where id=200);
+----+------------+--------+------+--------+
| id | name       | sex    | age  | dep_id |
+----+------------+--------+------+--------+
|  1 | egon       | male   |   18 |    200 |
|  2 | alex       | female |   48 |    201 |
|  3 | wupeiqi    | male   |   38 |    201 |
|  4 | yuanhao    | female |   28 |    202 |
|  5 | liwenzhou  | male   |   18 |    200 |
|  6 | jingliyang | female |   18 |    204 |
+----+------------+--------+------+--------+

#department表中存在dept_id=205,False
mysql> select * from employee
    ->     where exists
    ->         (select id from department where id=204);
Empty set (0.00 sec)

练习:

  • 查询每个部门最新入职的那位员工
1 #表与数据准备
 2 company.employee
 3     员工id      id                  int             
 4     姓名        emp_name            varchar
 5     性别        sex                 enum
 6     年龄        age                 int
 7     入职日期     hire_date           date
 8     岗位        post                varchar
 9     职位描述     post_comment        varchar
10     薪水        salary              double
11     办公室       office              int
12     部门编号     depart_id           int
13 
14 
15 
16 #创建表
17 create table employee(
18 id int not null unique auto_increment,
19 name varchar(20) not null,
20 sex enum('male','female') not null default 'male', #大部分是男的
21 age int(3) unsigned not null default 28,
22 hire_date date not null,
23 post varchar(50),
24 post_comment varchar(100),
25 salary double(15,2),
26 office int, #一个部门一个屋子
27 depart_id int
28 );
29 
30 
31 #查看表结构
32 mysql> desc employee;
33 +--------------+-----------------------+------+-----+---------+----------------+
34 | Field        | Type                  | Null | Key | Default | Extra          |
35 +--------------+-----------------------+------+-----+---------+----------------+
36 | id           | int(11)               | NO   | PRI | NULL    | auto_increment |
37 | name         | varchar(20)           | NO   |     | NULL    |                |
38 | sex          | enum('male','female') | NO   |     | male    |                |
39 | age          | int(3) unsigned       | NO   |     | 28      |                |
40 | hire_date    | date                  | NO   |     | NULL    |                |
41 | post         | varchar(50)           | YES  |     | NULL    |                |
42 | post_comment | varchar(100)          | YES  |     | NULL    |                |
43 | salary       | double(15,2)          | YES  |     | NULL    |                |
44 | office       | int(11)               | YES  |     | NULL    |                |
45 | depart_id    | int(11)               | YES  |     | NULL    |                |
46 +--------------+-----------------------+------+-----+---------+----------------+
47 
48 #插入记录
49 #三个部门:教学,销售,运营
50 insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
51 ('egon','male',18,'20170301','老男孩驻沙河办事处外交大使',7300.33,401,1), #以下是教学部
52 ('alex','male',78,'20150302','teacher',1000000.31,401,1),
53 ('wupeiqi','male',81,'20130305','teacher',8300,401,1),
54 ('yuanhao','male',73,'20140701','teacher',3500,401,1),
55 ('liwenzhou','male',28,'20121101','teacher',2100,401,1),
56 ('jingliyang','female',18,'20110211','teacher',9000,401,1),
57 ('jinxin','male',18,'19000301','teacher',30000,401,1),
58 ('成龙','male',48,'20101111','teacher',10000,401,1),
59 
60 ('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
61 ('丫丫','female',38,'20101101','sale',2000.35,402,2),
62 ('丁丁','female',18,'20110312','sale',1000.37,402,2),
63 ('星星','female',18,'20160513','sale',3000.29,402,2),
64 ('格格','female',28,'20170127','sale',4000.33,402,2),
65 
66 ('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
67 ('程咬金','male',18,'19970312','operation',20000,403,3),
68 ('程咬银','female',18,'20130311','operation',19000,403,3),
69 ('程咬铜','male',18,'20150411','operation',18000,403,3),
70 ('程咬铁','female',18,'20140512','operation',17000,403,3)
71 ;
72 
73 #ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk
  • View Code
  • 答案一:连表查询

    mysql多表查询count mysql多表查询实验总结_mysql

    mysql多表查询count mysql多表查询实验总结_子查询_02


    1 SELECT
     2     *
     3 FROM
     4     emp AS t1
     5 INNER JOIN (
     6     SELECT
     7         post,
     8         max(hire_date) max_date
     9     FROM
    10         emp
    11     GROUP BY
    12         post
    13 ) AS t2 ON t1.post = t2.post
    14 WHERE
    15     t1.hire_date = t2.max_date;


  • View Code

  • 答案二:子查询

    mysql多表查询count mysql多表查询实验总结_mysql

    mysql多表查询count mysql多表查询实验总结_子查询_02


    1 mysql> select (select t2.name from emp as t2 where t2.post=t1.post order by hire_date desc limit 1) from emp as t1 group by post;
     2 +---------------------------------------------------------------------------------------+
     3 | (select t2.name from emp as t2 where t2.post=t1.post order by hire_date desc limit 1) |
     4 +---------------------------------------------------------------------------------------+
     5 | 张野                                                                                  |
     6 | 格格                                                                                  |
     7 | alex                                                                                  |
     8 | egon                                                                                  |
     9 +---------------------------------------------------------------------------------------+
    10 rows in set (0.00 sec)
    11 
    12 mysql> select (select t2.id from emp as t2 where t2.post=t1.post order by hire_date desc limit 1) from emp as t1 group by post;
    13 +-------------------------------------------------------------------------------------+
    14 | (select t2.id from emp as t2 where t2.post=t1.post order by hire_date desc limit 1) |
    15 +-------------------------------------------------------------------------------------+
    16 |                                                                                  14 |
    17 |                                                                                  13 |
    18 |                                                                                   2 |
    19 |                                                                                   1 |
    20 +-------------------------------------------------------------------------------------+
    21 rows in set (0.00 sec)
    22 
    23 #正确答案
    24 mysql> select t3.name,t3.post,t3.hire_date from emp as t3 where id in (select (select id from emp as t2 where t2.post=t1.post order by hire_date desc limit 1) from emp as t1 group by post);
    25 +--------+-----------------------------------------+------------+
    26 | name   | post                                    | hire_date  |
    27 +--------+-----------------------------------------+------------+
    28 | egon   | 老男孩驻沙河办事处外交大使              | 2017-03-01 |
    29 | alex   | teacher                                 | 2015-03-02 |
    30 | 格格   | sale                                    | 2017-01-27 |
    31 | 张野   | operation                               | 2016-03-11 |
    32 +--------+-----------------------------------------+------------+
    33 rows in set (0.00 sec)


  • View Code

  • 答案一为正确答案,答案二中的limit 1有问题(每个部门可能有>1个为同一时间入职的新员工),我只是想用该例子来说明可以在select后使用子查询

    可以基于上述方法解决:比如某网站在全国各个市都有站点,每个站点一条数据,想取每个省下最新的那一条市的网站质量信息

综合练习

  • init.sql文件内容
1 /*
  2  数据导入:
  3  Navicat Premium Data Transfer
  4 
  5  Source Server         : localhost
  6  Source Server Type    : MySQL
  7  Source Server Version : 50624
  8  Source Host           : localhost
  9  Source Database       : sqlexam
 10 
 11  Target Server Type    : MySQL
 12  Target Server Version : 50624
 13  File Encoding         : utf-8
 14 
 15  Date: 10/21/2016 06:46:46 AM
 16 */
 17 
 18 SET NAMES utf8;
 19 SET FOREIGN_KEY_CHECKS = 0;
 20 
 21 -- ----------------------------
 22 --  Table structure for `class`
 23 -- ----------------------------
 24 DROP TABLE IF EXISTS `class`;
 25 CREATE TABLE `class` (
 26   `cid` int(11) NOT NULL AUTO_INCREMENT,
 27   `caption` varchar(32) NOT NULL,
 28   PRIMARY KEY (`cid`)
 29 ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
 30 
 31 -- ----------------------------
 32 --  Records of `class`
 33 -- ----------------------------
 34 BEGIN;
 35 INSERT INTO `class` VALUES ('1', '三年二班'), ('2', '三年三班'), ('3', '一年二班'), ('4', '二年九班');
 36 COMMIT;
 37 
 38 -- ----------------------------
 39 --  Table structure for `course`
 40 -- ----------------------------
 41 DROP TABLE IF EXISTS `course`;
 42 CREATE TABLE `course` (
 43   `cid` int(11) NOT NULL AUTO_INCREMENT,
 44   `cname` varchar(32) NOT NULL,
 45   `teacher_id` int(11) NOT NULL,
 46   PRIMARY KEY (`cid`),
 47   KEY `fk_course_teacher` (`teacher_id`),
 48   CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
 49 ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
 50 
 51 -- ----------------------------
 52 --  Records of `course`
 53 -- ----------------------------
 54 BEGIN;
 55 INSERT INTO `course` VALUES ('1', '生物', '1'), ('2', '物理', '2'), ('3', '体育', '3'), ('4', '美术', '2');
 56 COMMIT;
 57 
 58 -- ----------------------------
 59 --  Table structure for `score`
 60 -- ----------------------------
 61 DROP TABLE IF EXISTS `score`;
 62 CREATE TABLE `score` (
 63   `sid` int(11) NOT NULL AUTO_INCREMENT,
 64   `student_id` int(11) NOT NULL,
 65   `course_id` int(11) NOT NULL,
 66   `num` int(11) NOT NULL,
 67   PRIMARY KEY (`sid`),
 68   KEY `fk_score_student` (`student_id`),
 69   KEY `fk_score_course` (`course_id`),
 70   CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
 71   CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
 72 ) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;
 73 
 74 -- ----------------------------
 75 --  Records of `score`
 76 -- ----------------------------
 77 BEGIN;
 78 INSERT INTO `score` VALUES ('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87');
 79 COMMIT;
 80 
 81 -- ----------------------------
 82 --  Table structure for `student`
 83 -- ----------------------------
 84 DROP TABLE IF EXISTS `student`;
 85 CREATE TABLE `student` (
 86   `sid` int(11) NOT NULL AUTO_INCREMENT,
 87   `gender` char(1) NOT NULL,
 88   `class_id` int(11) NOT NULL,
 89   `sname` varchar(32) NOT NULL,
 90   PRIMARY KEY (`sid`),
 91   KEY `fk_class` (`class_id`),
 92   CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
 93 ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
 94 
 95 -- ----------------------------
 96 --  Records of `student`
 97 -- ----------------------------
 98 BEGIN;
 99 INSERT INTO `student` VALUES ('1', '男', '1', '理解'), ('2', '女', '1', '钢蛋'), ('3', '男', '1', '张三'), ('4', '男', '1', '张一'), ('5', '女', '1', '张二'), ('6', '男', '1', '张四'), ('7', '女', '2', '铁锤'), ('8', '男', '2', '李三'), ('9', '男', '2', '李一'), ('10', '女', '2', '李二'), ('11', '男', '2', '李四'), ('12', '女', '3', '如花'), ('13', '男', '3', '刘三'), ('14', '男', '3', '刘一'), ('15', '女', '3', '刘二'), ('16', '男', '3', '刘四');
100 COMMIT;
101 
102 -- ----------------------------
103 --  Table structure for `teacher`
104 -- ----------------------------
105 DROP TABLE IF EXISTS `teacher`;
106 CREATE TABLE `teacher` (
107   `tid` int(11) NOT NULL AUTO_INCREMENT,
108   `tname` varchar(32) NOT NULL,
109   PRIMARY KEY (`tid`)
110 ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
111 
112 -- ----------------------------
113 --  Records of `teacher`
114 -- ----------------------------
115 BEGIN;
116 INSERT INTO `teacher` VALUES ('1', '张磊老师'), ('2', '李平老师'), ('3', '刘海燕老师'), ('4', '朱云海老师'), ('5', '李杰老师');
117 COMMIT;
118 
119 SET FOREIGN_KEY_CHECKS = 1;
  • View Code
  • 从init.sql文件中导入数据
1 #准备表、记录
2 mysql> create database db1;
3 mysql> use db1;
4 mysql> source /root/init.sql
  • View Code

表结构为

mysql多表查询count mysql多表查询实验总结_mysql多表查询count_11

  • 练习题
1 1、查询男生、女生的人数;
 2 2、查询姓“张”的学生名单;
 3 3、课程平均分从高到低显示
 4 4、查询有课程成绩小于60分的同学的学号、姓名;
 5 5、查询至少有一门课与学号为1的同学所学课程相同的同学的学号和姓名;
 6 6、查询出只选修了一门课程的全部学生的学号和姓名;
 7 7、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
 8 8、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
 9 9、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
10 10、查询平均成绩大于60分的同学的学号和平均成绩;
11 11、查询所有同学的学号、姓名、选课数、总成绩;
12 12、查询姓“李”的老师的个数;
13 13、查询没学过“张磊老师”课的同学的学号、姓名;
14 14、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名;
15 15、查询学过“李平老师”所教的所有课的同学的学号、姓名;
16 16、查询没有学全所有课的同学的学号、姓名;
17 17、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;
18 18、删除学习“叶平”老师课的SC表记录;
19 19、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩; 
20 20、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
21 21、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
22 22、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
23 23、查询各科成绩前三名的记录:(不考虑成绩并列情况) 
24 24、查询每门课程被选修的学生数;
25 25、查询同名同姓学生名单,并统计同名人数;
26 26、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
27 27、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;
28 28、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
29 29、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名; 
30 30、求选了课程的学生人数
31 31、查询选修“杨艳”老师所授课程的学生中,成绩最高的学生姓名及其成绩;
32 32、查询各个课程及相应的选修人数;
33 33、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
34 34、查询每门课程成绩最好的前两名;
35 35、检索至少选修两门课程的学生学号;
36 36、查询全部学生都选修的课程的课程号和课程名;
37 37、查询没学过“叶平”老师讲授的任一门课程的学生姓名;
38 38、查询两门以上不及格课程的同学的学号及其平均成绩;
39 39、检索“004”课程分数小于60,按分数降序排列的同学学号;
40 40、删除“002”同学的“001”课程的成绩;
  • View Code