MySQL--学习记录4
目录
1.什么是内连接查询; 如何使用内连接查询。
编程要求
在右侧编辑器补充代码,查询数据表中学生姓名以及对应的班级名称,将其对应的列名分别另命名为studentName
和className
。
我们为你提供了两张表,内容如下:
tb_student
表数据:
id | name | class_id |
1 | Emma | 2 |
2 | Mary | 4 |
3 | Allen | (null) |
4 | Kevin | 1 |
5 | Rose | 2 |
6 | James | 1 |
tb_class
表数据:
id | name |
1 | 软件1631 |
2 | 软件1632 |
3 | 测试1631 |
4 | 测试1632 |
create table tb_student
(
id int,
name varchar(25),
class_id int
);
insert into tb_student (id,name,class_id)
values (1,'Emma',2),
(2,'Mary',4),
(3,'Allen',null),
(4,'Kevin',1),
(5,'Rose',2),
(6,'James',1);
create table tb_class
(
id int,
name varchar(25)
);
insert into tb_class (id,name)
values (1,'软件1631'),
(2,'软件1632'),
(3,'测试1631'),
(4,'测试1632');
select tb_stu.name as studentName ,tb_cla.name as className from tb_class as tb_cla join tb_student as tb_stu on tb_stu.class_id=tb_cla.id;
2.什么是外连接查询; 如何使用外连接查询。
编程要求
在右侧编辑器补充代码,分别使用左外连接
和右外连接
查询数据表中所有学生姓名和对应的班级名称,查询结果列分别另命名为studentName
和className
。
我们为你提供了两张表,内容如下:
tb_student
表数据:
id | name | class_id |
1 | Emma | 2 |
2 | Mary | 4 |
3 | Allen | (null) |
4 | Kevin | 1 |
5 | Rose | 2 |
6 | James | 1 |
tb_class
表数据:
id | name |
1 | 软件1631 |
2 | 软件1632 |
3 | 测试1631 |
4 | 测试1632 |
select tb_stu.name as studentName ,
tb_cla.name as className
from tb_class as tb_cla left
join tb_student as tb_stu on tb_stu.class_id=tb_cla.id;
select tb_stu.name as studentName ,
tb_cla.name as className
from tb_class as tb_cla right
join tb_student as tb_stu on tb_stu.class_id=tb_cla.id;
3.如何编写复合条件连接查询语句
编程要求
在右侧编辑器补充代码,查询所有班级里分数在90
分以上的学生的姓名和学生的成绩以及学生所在的班级,其中学生的姓名和学生所在班级分别另命名为studentName
和className
。
我们为你提供了两张表,内容如下:
tb_student
表数据:
id | name | class_id | score |
1 | Emma | 2 | 89 |
2 | Mary | 4 | 92 |
4 | Kevin | 1 | 76 |
5 | Rose | 3 | 68 |
6 | James | 1 | 99 |
tb_class
表数据:
id | name |
1 | 软件1631 |
2 | 软件1632 |
3 | 测试1631 |
4 | 测试1632 |
create table tb_student2
(
id int,
name varchar(25),
class_id int,
id_score int
);
insert into tb_student2 (id,name,class_id,id_score)
values (1,'Emma',2,89),
(2,'Mary',4,92),
(4,'Kevin',1,76),
(5,'Rose',2,68),
(6,'James',1,99);
/*查询所有班级里分数在90分以上的学生的姓名和学
生的成绩以及学生所在的班级,其中学生的姓名和学生
所在班级分别另命名为studentName和className。*/
select tb_stu2.name as studentName ,
tb_stu2.id_score,tb_cla.name as className
from tb_class as tb_cla
join tb_student2 as tb_stu2 on tb_stu2.class_id=tb_cla.id and tb_stu2.id_score>=90;
4.什么叫子查询, 比较运算符结合标量子查询的使用。
编程要求
在右侧编辑器补充代码,查询大于所有平均年龄的员工姓名与年龄。
我们为你提供了tb_emp
表,数据如下:
id | name | age |
1 | Mary | 23 |
2 | Allen | 21 |
3 | kevin | 25 |
4 | Tom | 33 |
5 | Nancy | 28 |
create table tb_emp
(
id int,
name varchar(25),
age int
);
insert into tb_emp (id,name,age)
values (1,'Mary',23),
(2,'Allen',21),
(3,'kevin',25),
(4,'Tom',33),
(5,'Nancy',28);
/*查询大于所有平均年龄的员工姓名与年龄。*/
select name,age
from tb_emp
where age>(select avg(age) from tb_emp);
5.如何在子查询中使用关键字进行查询。
编程要求
我们为你提供了如下数据表: tb_salary
表数据:
id | position | salary |
1 | Java | 8000 |
2 | Java | 8400 |
3 | Java | 9000 |
4 | Python | 6500 |
5 | Python | 10000 |
根据提供的数据,在右侧编辑器中补充代码:
查询薪资表中比Java
最高工资高的所有员工职位名称和薪资;
查询薪资表中比Java
最低工资高的所有员工职位名称和薪资;
查询薪资表中职位为Java
的所有员工职位名称和薪资。
create table tb_salary
(
id int,
position varchar(25),
salary int
);
insert into tb_salary (id,position,salary)
values
(1,'Java',8000),
(2,'Java',8400),
(3,'Java',9000),
(4,'Python',6500),
(5,'Python',10000);
/*查询薪资表中比Java最高工资高的所有员工职位名称和薪资;
查询薪资表中比Java最低工资高的所有员工职位名称和薪资;
查询薪资表中职位为Java的所有员工职位名称和薪资。*/
select position,salary
from tb_salary
where salary > any(select max(salary) from tb_salary where position='java');
select position,salary
from tb_salary
where salary > any(select min(salary) from tb_salary where position='java');
select position,salary
from tb_salary
where position='java';