union.md 1.3 KB
Newer Older
M
union  
Mars Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# Union

现有员工信息表和顾客信息表如下

```mysql
create table employee(
    id int primary key auto_increment,
    name varchar(256),
    address varchar(1024),
    dept varchar(64)
    -- ignore more
);

create table customer(
                         id int primary key auto_increment,
                         name varchar(256),
                         address varchar(1024),
                         level int
    -- ignore more
)

```

Joe 需要员工和顾客的联系方式(姓名+地址)清单,用于邮寄礼品。这个查询如何写?

M
Mars Liu 已提交
26 27
<hr/>

F
feilong 已提交
28
点击进入[MySQL实战练习环境](https://mydev.csdn.net/product/pod/new?image=cimg-centos7-skilltreemysql&connect=auto&create=auto&utm_source=skill){target="_blank"}。
F
feilong 已提交
29

F
feilong 已提交
30 31
* `show databases;` 列出所有数据库
* `show tables;` 列出所有表
M
Mars Liu 已提交
32

M
union  
Mars Liu 已提交
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 75 76 77 78 79 80 81 82 83 84
## 答案

```mysql
select name, address 
from customer
union
select name, address
from employee
```

## 选项

### A

```mysql
select * 
from customer
union
select *
from employee
```

### B

```mysql
select * 
from customer
join employee
```

### C

```mysql
select * 
from customer
join employee on customer.id = employee.id
```

### D

```mysql
select * 
from customer, employee
```

### E

```mysql
select name, address 
from customer, employee
```