union.md 1.2 KB
Newer Older
M
union  
Mars Liu 已提交
1 2 3 4
# Union

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

fix bug  
张志晨 已提交
5
```sql
M
union  
Mars Liu 已提交
6 7 8 9 10 11 12 13 14
create table employee(
    id int primary key auto_increment,
    name varchar(256),
    address varchar(1024),
    dept varchar(64)
    -- ignore more
);

create table customer(
15 16 17 18
     id int primary key auto_increment,
     name varchar(256),
     address varchar(1024),
     level int
M
union  
Mars Liu 已提交
19 20 21 22 23 24 25
    -- 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
## 答案

fix bug  
张志晨 已提交
35
```sql
M
union  
Mars Liu 已提交
36 37 38 39 40 41 42 43 44 45 46
select name, address 
from customer
union
select name, address
from employee
```

## 选项

### A

fix bug  
张志晨 已提交
47
```sql
M
union  
Mars Liu 已提交
48 49 50 51 52 53 54 55 56
select * 
from customer
union
select *
from employee
```

### B

fix bug  
张志晨 已提交
57
```sql
M
union  
Mars Liu 已提交
58 59 60 61 62 63 64
select * 
from customer
join employee
```

### C

fix bug  
张志晨 已提交
65
```sql
M
union  
Mars Liu 已提交
66 67 68 69 70 71 72
select * 
from customer
join employee on customer.id = employee.id
```

### D

fix bug  
张志晨 已提交
73
```sql
M
union  
Mars Liu 已提交
74 75 76 77 78 79
select * 
from customer, employee
```

### E

fix bug  
张志晨 已提交
80
```sql
M
union  
Mars Liu 已提交
81 82 83 84
select name, address 
from customer, employee
```