0
点赞
收藏
分享

微信扫一扫

MySQL--学习记录4

残北 2022-04-22 阅读 118

MySQL--学习记录4

目录

1.什么是内连接查询; 如何使用内连接查询。

2.什么是外连接查询; 如何使用外连接查询。

 3.如何编写复合条件连接查询语句

4.什么叫子查询, 比较运算符结合标量子查询的使用。

5.如何在子查询中使用关键字进行查询。


1.什么是内连接查询; 如何使用内连接查询。

编程要求

在右侧编辑器补充代码,查询数据表中学生姓名以及对应的班级名称,将其对应的列名分别另命名为studentNameclassName

我们为你提供了两张表,内容如下:

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.什么是外连接查询; 如何使用外连接查询。

编程要求

在右侧编辑器补充代码,分别使用左外连接右外连接查询数据表中所有学生姓名和对应的班级名称,查询结果列分别另命名为studentNameclassName

我们为你提供了两张表,内容如下:

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分以上的学生的姓名和学生的成绩以及学生所在的班级,其中学生的姓名和学生所在班级分别另命名为studentNameclassName

我们为你提供了两张表,内容如下:

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';

举报

相关推荐

MySQL--学习记录1

MySQL--索引

MYSQL--事务

mysql--事务

MySQL-- 表操作

MySQL--刷题

MySQL--基础--事务

MySQL--日志管理

0 条评论