cross_join.md 1.4 KB
Newer Older
M
join  
Mars Liu 已提交
1 2 3 4
# Cross Join

Joe 需要生成 goods 表

fix bug  
张志晨 已提交
5
```sql
M
join  
Mars Liu 已提交
6 7 8 9 10 11 12 13 14 15 16 17
create table goods(
    id int primary key auto_increment,
    category varchar(64),
    name varchar(256),
    price decimal(12, 4),
    stock int,
    upper_time timestamp
)
```

中所有T恤(category为`T-Shirt`)的所有尺寸,尺寸信息在 size 表

fix bug  
张志晨 已提交
18
```sql
M
join  
Mars Liu 已提交
19 20 21 22 23 24 25 26
create table size(
    id int primary key auto_increment,
    name varchar(16)
)
```

中,那么这个查询应该是:

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

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

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

M
join  
Mars Liu 已提交
34 35
## 答案

fix bug  
张志晨 已提交
36
```sql
M
join  
Mars Liu 已提交
37 38 39 40 41 42 43 44 45 46
select g.id, g.name, s.name as size
from goods as g
    cross join size as s
where g.category = 'T-Shirt';
```

## 选项

### A

fix bug  
张志晨 已提交
47
```sql
M
join  
Mars Liu 已提交
48 49 50 51 52 53 54 55
select g.id, g.name, s.name as size
from goods as g
    full join size as s
where g.category = 'T-Shirt';
```

### B

fix bug  
张志晨 已提交
56
```sql
M
join  
Mars Liu 已提交
57 58 59 60 61 62 63 64
select g.id, g.name, s.name as size
from goods as g
    left join size as s
where g.category = 'T-Shirt';
```

### C

fix bug  
张志晨 已提交
65
```sql
M
join  
Mars Liu 已提交
66 67 68 69 70 71 72 73
select g.id, g.name, s.name as size
from goods as g
    left join size as s
where g.category = 'T-Shirt';
```

### D

fix bug  
张志晨 已提交
74
```sql
M
join  
Mars Liu 已提交
75 76 77 78 79 80 81 82
select g.id, g.name, s.name as size
from goods as g
    right join size as s
where g.category = 'T-Shirt';
```

### E

fix bug  
张志晨 已提交
83
```sql
M
join  
Mars Liu 已提交
84 85 86 87 88 89 90
select g.id, g.name, s.name as size
from goods as g
    outter join size as s
where g.category = 'T-Shirt';
```