right_join.md 1.3 KB
Newer Older
M
join  
Mars Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 右连接

现有部门表

```mysql
create table department(
    id int primary key auto_increment,
    name varchar(256)
)
```

和员工表

```mysql
create table employee(
    id int primary key auto_increment,
M
Mars Liu 已提交
17
    dept_id int,
M
join  
Mars Liu 已提交
18 19 20 21 22 23 24 25
    name varchar(256),
    post varchar(16)
)
```

公司经过了一轮调整后,员工信息有些混乱,现在 Joe 要写一个查询,找出部门信息写
错的员工,这些员工所在的部门在 department 表中没有对应记录。

M
Mars Liu 已提交
26 27
点击进入[MySQL实战练习环境](https://mydev.csdn.net/product/pod/new?image=cimg-centos7-skilltreemysql&connect=auto&create=auto&utm_source=skill)

M
join  
Mars Liu 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
## 答案

```mysql
select e.id, e.name, e.dept
from department as d
    right join employee as e on d.id = e.dept
where d.id is null;
```

## 选项

### A

```mysql
select e.id, e.name, e.dept
from employee as e
         right join department as d on d.id = e.dept
where e.id is null;
```

### B

```mysql
select e.id, e.name, e.dept
from employee as e
         right join department as d on d.id = e.dept
where d.id is null;
```

### C

```mysql
select e.id, e.name, e.dept
from department as d
    join employee as e on d.id = e.dept
where d.id is null;
```


### D

```mysql
select e.id, e.name, e.dept
from department as d
    right join employee as e on d.id = e.dept
where d.id is null;
```