查询语句
一、单表查询
素材: 表名:worker-- 表中字段均为中文,比如 部门号 工资 职工号 参加工作 等
CREATE TABLE `worker` (
`部门号` int(11) NOT NULL, #这里的11代表数字的长度,也就是最多可以存储11个数字,以便在未来需要扩展它时有足够的空间
`职工号` int(11) NOT NULL,
`工作时间` date NOT NULL,
`工资` float(8,2) NOT NULL,
`政治面貌` varchar(10) NOT NULL DEFAULT '群众',
`姓名` varchar(20) NOT NULL,
`出生日期` date NOT NULL,
PRIMARY KEY (`职工号`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (101, 1001, '2015-5-4', 3500.00, '群众', '张三', '1990-7-1');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (101, 1002, '2017-2-6', 3200.00, '团员', '李四', '1997-2-8');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1003, '2011-1-4', 8500.00, '党员', '王亮', '1983-6-8');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1004, '2016-10-10', 5500.00, '群众', '赵六', '1994-9-5');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1005, '2014-4-1', 4800.00, '党员', '钱七', '1992-12-30');
INSERT INTO `worker` (`部门号`, `职工号`, `工作时间`, `工资`, `政治面貌`, `姓名`, `出生
日期`) VALUES (102, 1006, '2017-5-5', 4500.00, '党员', '孙八', '1996-9-2');
1、显示所有职工的基本信息。
2、查询所有职工所属部门的部门号,不显示重复的部门号。
3、求出所有职工的人数。
4、列出最高工和最低工资。
5、列出职工的平均工资和总工资。
6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。
7、显示所有女职工的年龄。
8、列出所有姓刘的职工的职工号、姓名和出生日期。
9、列出1960年以前出生的职工的姓名、参加工作日期。
10、列出工资在1000-2000之间的所有职工姓名。
11、列出所有陈姓和李姓的职工姓名。
12、列出所有部门号为2和3的职工号、姓名、党员否。
13、将职工表worker中的职工按出生的先后顺序排序。
14、显示工资最高的前3名职工的职工号和姓名。
15、求出各部门党员的人数。
16、统计各部门的工资和平均工资
17、列出总人数大于4的部门号和总人数。
#创建worker表
CREATE TABLE `worker` (
`部门号` int NOT NULL,
`职工号` int NOT NULL,
`工作时间` date NOT NULL,
`工资` float(8,2) NOT NULL,
`政治面貌` varchar(10) NOT NULL DEFAULT '群众',
`姓名` varchar(20) NOT NULL,
`出生日期` date NOT NULL,
PRIMARY KEY (`职工号`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
#1、显示所有职工的基本信息
mysql> select *from worker;
+-----------+-----------+--------------+---------+--------------+-----------+--------------+
| 部门号 | 职工号 | 工作时间 | 工资 | 政治面貌 | 姓名 | 出生日期 |
+-----------+-----------+--------------+---------+--------------+-----------+--------------+
| 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 |
| 101 | 1002 | 2017-02-06 | 3200.00 | 团员 | 李四 | 1997-02-08 |
| 102 | 1003 | 2011-01-04 | 8500.00 | 党员 | 王亮 | 1983-06-08 |
| 102 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵六 | 1994-09-05 |
| 102 | 1005 | 2014-04-01 | 4800.00 | 党员 | 钱七 | 1992-12-30 |
| 102 | 1006 | 2017-05-05 | 4500.00 | 党员 | 孙八 | 1996-09-02 |
| 102 | 1007 | 2014-04-01 | 4800.00 | 党员 | 孙九 | 1960-12-30 |
| 102 | 1008 | 2014-04-01 | 4800.00 | 党员 | lily | 1960-12-30 |
| 102 | 1009 | 2014-04-01 | 4800.00 | 党员 | 陈平安 | 1960-12-30 |
| 102 | 1010 | 2015-05-04 | 4900.00 | 群众 | 张朗 | 1970-12-12 |
+-----------+-----------+--------------+---------+--------------+-----------+--------------+
#2、查询所有职工所属部门的部门号,不显示重复的部门号
mysql> select distinct 部门号 from worker;
+-----------+
| 部门号 |
+-----------+
| 101 |
| 102 |
+-----------+
#3、求出所有职工的人数
mysql> select count(职工号) from worker;
+------------------+
| count(职工号) |
+------------------+
| 10 |
+------------------+
#4、列出最高工和最低工资
mysql> select max(工资) as 最高工资,min(工资) as 最低工资 from worker;
+--------------+--------------+
| 最高工资 | 最低工资 |
+--------------+--------------+
| 8500.00 | 3200.00 |
+--------------+--------------+
#5、列出职工的平均工资和总工资
mysql> select avg(工资) as 平均工资,sum(工资) as 总工资 from worker;
+--------------+-----------+
| 平均工资 | 总工资 |
+--------------+-----------+
| 4930.000000 | 49300.00 |
+--------------+-----------+
#6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表
mysql> create table worker_date select 职工号,姓名,工作时间 from worker;
mysql> select *from worker_date;
+-----------+--------+--------------+
| 职工号 | 姓名 | 工作时间 |
+-----------+--------+--------------+
| 1001 | 张三 | 2015-05-04 |
| 1002 | 李四 | 2017-02-06 |
| 1003 | 王亮 | 2011-01-04 |
| 1004 | 赵六 | 2016-10-10 |
| 1005 | 钱七 | 2014-04-01 |
| 1006 | 孙八 | 2017-05-05 |
+-----------+--------+--------------+
#8、列出所有姓刘的职工的职工号、姓名和出生日期
mysql> select 职工号,姓名,出生日期 from worker where 姓名 like '刘%';
+-----------+--------+--------------+
| 职工号 | 姓名 | 出生日期 |
+-----------+--------+--------------+
| 1011 | 刘牛 | 1977-12-04 |
+-----------+--------+--------------+
#9、列出1960年以前出生的职工的姓名、参加工作日期
mysql> select 姓名,工作时间 from worker where 出生日期<'1960-01-01';
+--------+--------------+
| 姓名 | 工作时间 |
+--------+--------------+
| 曹操 | 2014-02-01 |
+--------+--------------+
#10、列出工资在1000-2000之间的所有职工姓名
mysql> select 姓名 from worker where 工资 between 1000 and 2000;
+--------+
| 姓名 |
+--------+
| mick |
+--------+
1 row in set (0.04 sec)
mysql> select 姓名 from worker where 工资>=1000 and 工资<=2000;
+--------+
| 姓名 |
+--------+
| mick |
+--------+
1 row in set (0.04 sec)
#11、列出所有陈姓和李姓的职工姓名
mysql> select 姓名 from worker where 姓名 like '陈%' or 姓名 like '李%';
+-----------+
| 姓名 |
+-----------+
| 李四 |
| 陈平安 |
| 李粒 |
+-----------+
mysql> select 姓名 from worker where 姓名 regexp '^李|^陈';
+-----------+
| 姓名 |
+-----------+
| 李四 |
| 陈平安 |
| 李粒 |
+-----------+
#12、列出所有部门号为2和3的职工号、姓名、党员否
三种查询方式如下
mysql> select 职工号,姓名,政治面貌 from worker where 部门号 in(102,103);
mysql> select 职工号,姓名,政治面貌 from worker where 部门号=102 or 部门号=103;
mysql> select 职工号,姓名,政治面貌 from worker where 部门号=102
-> union
-> select 职工号,姓名,政治面貌 from worker where 部门号=103;
mysql> select 职工号,姓名,IF(政治面貌='党员','是','否') as 党员 from worker where 部门号 in(102,103);
+-----------+-----------+--------+
| 职工号 | 姓名 | 党员 |
+-----------+-----------+--------+
| 1003 | 王亮 | 是 |
| 1004 | 赵六 | 否 |
| 1005 | 钱七 | 是 |
| 1006 | 孙八 | 是 |
| 1007 | 孙九 | 是 |
| 1008 | lily | 是 |
| 1009 | 陈平安 | 是 |
| 1010 | 张朗 | 否 |
| 1011 | 刘牛 | 否 |
| 1012 | 曹操 | 否 |
| 1013 | mick | 否 |
+-----------+-----------+--------+
#13、将职工表worker中的职工按出生的先后顺序排序
mysql> select *from worker order by 出生日期 asc;
+-----------+-----------+--------------+---------+--------------+-----------+--------------+
| 部门号 | 职工号 | 工作时间 | 工资 | 政治面貌 | 姓名 | 出生日期 |
+-----------+-----------+--------------+---------+--------------+-----------+--------------+
| 102 | 1012 | 2014-02-01 | 4800.00 | 群众 | 曹操 | 1956-12-04 |
| 102 | 1013 | 2014-02-01 | 1800.00 | 群众 | mick | 1956-12-04 |
| 102 | 1007 | 2014-04-01 | 4800.00 | 党员 | 孙九 | 1960-12-30 |
| 102 | 1008 | 2014-04-01 | 4800.00 | 党员 | lily | 1960-12-30 |
| 102 | 1009 | 2014-04-01 | 4800.00 | 党员 | 陈平安 | 1960-12-30 |
| 102 | 1010 | 2015-05-04 | 4900.00 | 群众 | 张朗 | 1970-12-12 |
| 102 | 1011 | 2014-02-01 | 4800.00 | 群众 | 刘牛 | 1977-12-04 |
| 102 | 1003 | 2011-01-04 | 8500.00 | 党员 | 王亮 | 1983-06-08 |
| 101 | 1014 | 2016-01-28 | 3800.00 | 党员 | 李粒 | 1990-02-23 |
| 101 | 1001 | 2015-05-04 | 3500.00 | 群众 | 张三 | 1990-07-01 |
| 102 | 1005 | 2014-04-01 | 4800.00 | 党员 | 钱七 | 1992-12-30 |
| 102 | 1004 | 2016-10-10 | 5500.00 | 群众 | 赵六 | 1994-09-05 |
| 102 | 1006 | 2017-05-05 | 4500.00 | 党员 | 孙八 | 1996-09-02 |
| 101 | 1002 | 2017-02-06 | 3200.00 | 团员 | 李四 | 1997-02-08 |
+-----------+-----------+--------------+---------+--------------+-----------+--------------+
#14、显示工资最高的前3名职工的职工号和姓名
mysql> select 职工号,姓名 from worker order by 工资 desclimit 3;
+-----------+--------+
| 职工号 | 姓名 |
+-----------+--------+
| 1003 | 王亮 |
| 1004 | 赵六 |
| 1010 | 张朗 |
+-----------+--------+
#15、求出各部门党员的人数
mysql> select 部门号,count(职工号) from worker where 政治面貌='党员' group by 部门号;
+-----------+------------------+
| 部门号 | count(职工号) |
+-----------+------------------+
| 102 | 6 |
| 101 | 1 |
+-----------+------------------+
#16、统计各部门的工资和平均工资
mysql> select 部门号,sum(工资),avg(工资) from worker groupp by 部门号;
+-----------+-------------+-------------+
| 部门号 | sum(工资) | avg(工资) |
+-----------+-------------+-------------+
| 101 | 10500.00 | 3500.000000 |
| 102 | 54000.00 | 4909.090909 |
+-----------+-------------+-------------+
#17、列出总人数大于4的部门号和总人数
mysql> select 部门号,count(职工号) as 职工人数 from worker
r group by 部门号 having 职工人数>4;
+-----------+--------------+
| 部门号 | 职工人数 |
+-----------+--------------+
| 102 | 11 |
+-----------+--------------+
二、多表查询
1.创建student和score表
CREATE TABLE student (
id INT(10) NOT NULL UNIQUE PRIMARY KEY ,
name VARCHAR(20) NOT NULL ,
sex VARCHAR(4) ,
birth YEAR,
department VARCHAR(20) ,
address VARCHAR(50)
);
创建score表。SQL代码如下:
CREATE TABLE score (
id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT ,
stu_id INT(10) NOT NULL ,
c_name VARCHAR(20) ,
grade INT(10)
);
2.为student表和score表增加记录
向student表插入记录的INSERT语句如下:
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
向score表插入记录的INSERT语句如下:
INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);
3.查询student表的所有记录
4.查询student表的第2条到4条记录
5.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
6.从student表中查询计算机系和英语系的学生的信息
7.从student表中查询年龄18~22岁的学生信息
8.从student表中查询每个院系有多少人
9.从score表中查询每个科目的最高分
10.查询李四的考试科目(c_name)和考试成绩(grade)
11.用连接的方式查询所有学生的信息和考试信息
12.计算每个学生的总成绩
13.计算每个考试科目的平均成绩
14.查询计算机成绩低于95的学生信息
15.查询同时参加计算机和英语考试的学生的信息
16.将计算机考试成绩按从高到低进行排序
17.从student表和score表中查询出学生的学号,然后合并查询结果
18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
#创建student表和score表
CREATE TABLE `student` (
`id` int NOT NULL,
`name` varchar(20) NOT NULL,
`sex` varchar(4) DEFAULT NULL,
`birth` year DEFAULT NULL,
`department` varchar(20) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `score` (
`id` int NOT NULL AUTO_INCREMENT,
`stu_id` int NOT NULL,
`c_name` varchar(20) DEFAULT NULL,
`grade` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
#3.查询student表的所有记录
mysql> select id,name,sex,birth,department,address from student;
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | birth | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+------+-------+--------------+--------------------+
4.查询student表的第2条到4条记录
mysql> select id,name,sex,birth,department,address from student limit 1,3;
+-----+-----------+------+-------+------------+--------------------+
| id | name | sex | birth | department | address |
+-----+-----------+------+-------+------------+--------------------+
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
+-----+-----------+------+-------+------------+--------------------+
5.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
mysql> select id,name,department from student;
+-----+-----------+--------------+
| id | name | department |
+-----+-----------+--------------+
| 901 | 张老大 | 计算机系 |
| 902 | 张老二 | 中文系 |
| 903 | 张三 | 中文系 |
| 904 | 李四 | 英语系 |
| 905 | 王五 | 英语系 |
| 906 | 王六 | 计算机系 |
+-----+-----------+--------------+
6.从student表中查询计算机系和英语系的学生的信息
mysql> select id,name,sex,birth,department,address from student where department='计算机系'
-> union
-> select id,name,sex,birth,department,address from student where department='英语系';
+-----+-----------+------+-------+--------------+--------------------+
| id | name | sex | birth | department | address |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
+-----+-----------+------+-------+--------------+--------------------+
7.从student表中查询年龄18~22岁的学生信息
mysql> select * from student where 2023-birth>=18 and 2023-birth<==22;
+-----+--------+------+-------+--------------+-----------------+
| id | name | sex | birth | department | address |
+-----+--------+------+-------+--------------+-----------------+
| 907 | 小红 | 女 | 2004 | 计算机系 | 上海浦东区 |
+-----+--------+------+-------+--------------+-----------------+
8.从student表中查询每个院系有多少人
mysql> select department,count(id) as 院系人数 from student group by department;
+--------------+--------------+
| department | 院系人数 |
+--------------+--------------+
| 计算机系 | 3 |
| 中文系 | 2 |
| 英语系 | 2 |
+--------------+--------------+
9.从score表中查询每个科目的最高分
mysql> select c_name,max(grade) as 科目最高分 from score group by
c_name;
+-----------+-----------------+
| c_name | 科目最高分 |
+-----------+-----------------+
| 英语 | 94 |
| 计算机 | 90 |
| 中文 | 95 |
+-----------+-----------------+
10.查询李四的考试科目(c_name)和考试成绩(grade)
mysql> select c_name,grade from score inner join student on student.id=score.stu_id where name='李四';
+-----------+-------+
| c_name | grade |
+-----------+-------+
| 计算机 | 70 |
| 英语 | 92 |
+-----------+-------+
#如下是在连接之前进行过滤
mysql> select c_name,grade from score inner join (select *from student where name='李四') as student on student.id=score.stu_id;
+-----------+-------+
| c_name | grade |
+-----------+-------+
| 计算机 | 70 |
| 英语 | 92 |
+-----------+-------+
11.用连接的方式查询所有学生的信息和考试信息
mysql> select student.*,score.* from score inner join student on student.id=score.stu_id;
+-----+-----------+------+-------+--------------+--------------------+----+--------+-----------+-------+
| id | name | sex | birth | department | address | id | stu_id | c_name | grade |
+-----+-----------+------+-------+--------------+--------------------+----+--------+-----------+-------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 | 1 | 901 | 英语 | 80 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 2 | 902 | 计算机 | 65 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 3 | 902 | 中文 | 88 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 | 4 | 903 | 中文 | 95 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 5 | 904 | 计算机 | 70 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 6 | 904 | 英语 | 92 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 | 7 | 905 | 英语 | 94 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 8 | 906 | 计算机 | 90 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 9 | 906 | 英语 | 85 |
+-----+-----------+------+-------+--------------+--------------------+----+--------+-----------+-------+
12.计算每个学生的总成绩
mysql> select student.name,sum(grade) from score inner join student on student.id=score.stu_id group by student.name;
+-----------+------------+
| name | sum(grade) |
+-----------+------------+
| 张老大 | 80 |
| 张老二 | 153 |
| 张三 | 95 |
| 李四 | 162 |
| 王五 | 94 |
| 王六 | 175 |
+-----------+------------+
mysql> select t1.name,sum(t1.grade) from (select student.id,student.name,score.c_name,score.grade from score inner join student on student.id=score.stu_id) as t1 group by t1.name;
+-----------+---------------+
| name | sum(t1.grade) |
+-----------+---------------+
| 张老大 | 80 |
| 张老二 | 153 |
| 张三 | 95 |
| 李四 | 162 |
| 王五 | 94 |
| 王六 | 175 |
+-----------+---------------+
13.计算每个考试科目的平均成绩
mysql> select c_name,avg(grade) as 平均成绩 from score group by c_
_name;
+-----------+--------------+
| c_name | 平均成绩 |
+-----------+--------------+
| 英语 | 87.7500 |
| 计算机 | 75.0000 |
| 中文 | 91.5000 |
+-----------+--------------+
14.查询计算机成绩低于95的学生信息
mysql> select *from student inner join score on student.id=score.stu_id where score.c_name='计算机' and score.grade<95;
+-----+-----------+------+-------+--------------+--------------------+----+--------+-----------+-------+
| id | name | sex | birth | department | address | id | stu_id | c_name | grade |
+-----+-----------+------+-------+--------------+--------------------+----+--------+-----------+-------+
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 2 | 902 | 计算机 | 65 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 5 | 904 | 计算机 | 70 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 8 | 906 | 计算机 | 90 |
+-----+-----------+------+-------+--------------+--------------------+----+--------+-----------+-------+
15.查询同时参加计算机和英语考试的学生的信息
#交叉连接:
mysql> select *from student where id in (select t1.stu_id from score as t1,score as t2 where t1.stu_id=t2.stu_id and t1.c_name='计算机' and t2.c_name='英语');
+-----+--------+------+-------+--------------+--------------------+
| id | name | sex | birth | department | address |
+-----+--------+------+-------+--------------+--------------------+
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+--------+------+-------+--------------+--------------------+
#内连接:(查询范围更小一些)
mysql> select *from student where id in (select t1.stu_id from (select stu_id from score where c_name='计算机') as t1 inner join (select stu_id from score where c_name='英语') as t2 on t1.stu_id=t2.stu_id);
+-----+--------+------+-------+--------------+--------------------+
| id | name | sex | birth | department | address |
+-----+--------+------+-------+--------------+--------------------+
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+--------+------+-------+--------------+--------------------+
16.将计算机考试成绩按从高到低进行排序
mysql> select grade as 计算机考试成绩 from score where c_name='计算机' order by grade desc;
+-----------------------+
| 计算机考试成绩 |
+-----------------------+
| 90 |
| 70 |
| 65 |
+-----------------------+
17.从student表和score表中查询出学生的学号,然后合并查询结果
mysql> select id from student
-> union all
-> select stu_id from score;
+-----+
| id |
+-----+
| 901 |
| 902 |
| 903 |
| 904 |
| 905 |
| 906 |
| 907 |
| 901 |
| 902 |
| 902 |
| 903 |
| 904 |
| 904 |
| 905 |
| 906 |
| 906 |
+-----+
18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
mysql> select student.name,student.department,score.c_name,score.grade from student inner join score on student.id=score.stu_id where student.name regexp '^张|^王';
+-----------+--------------+-----------+-------+
| name | department | c_name | grade |
+-----------+--------------+-----------+-------+
| 张老大 | 计算机系 | 英语 | 80 |
| 张老二 | 中文系 | 计算机 | 65 |
| 张老二 | 中文系 | 中文 | 88 |
| 张三 | 中文系 | 中文 | 95 |
| 王五 | 英语系 | 英语 | 94 |
| 王六 | 计算机系 | 计算机 | 90 |
| 王六 | 计算机系 | 英语 | 85 |
+-----------+--------------+-----------+-------+
19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
mysql> select student.name,2023-birth as 年龄,student.department,score.c_name,score.grade,student.address from student inner join score on student.id=score.stu_id where student.address like '湖南%';
+--------+--------+--------------+-----------+-------+--------------------+
| name | 年龄 | department | c_name | grade | address |
+--------+--------+--------------+-----------+-------+--------------------+
| 张三 | 33 | 中文系 | 中文 | 95 | 湖南省永州市 |
| 王六 | 35 | 计算机系 | 计算机 | 90 | 湖南省衡阳市 |
| 王六 | 35 | 计算机系 | 英语 | 85 | 湖南省衡阳市 |
+--------+--------+--------------+-----------+-------+--------------------+