cross_join.md 1.4 KB
Newer Older
M
join  
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 26
# Cross Join

Joe 需要生成 goods 表

```mysql
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 表

```mysql
create table size(
    id int primary key auto_increment,
    name varchar(16)
)
```

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

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

M
Mars Liu 已提交
29
点击进入[MySQL实战练习环境](https://mydev.csdn.net/product/pod/new?image=cimg-centos7-skilltreemysql&connect=auto&create=auto&utm_source=skill)
M
Mars Liu 已提交
30 31
* `show databases` 列出所有数据库
* `show tables` 列出所有表
M
Mars Liu 已提交
32

M
join  
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 85 86 87 88 89
## 答案

```mysql
select g.id, g.name, s.name as size
from goods as g
    cross join size as s
where g.category = 'T-Shirt';
```

## 选项

### A

```mysql
select g.id, g.name, s.name as size
from goods as g
    full join size as s
where g.category = 'T-Shirt';
```

### B

```mysql
select g.id, g.name, s.name as size
from goods as g
    left join size as s
where g.category = 'T-Shirt';
```

### C

```mysql
select g.id, g.name, s.name as size
from goods as g
    left join size as s
where g.category = 'T-Shirt';
```

### D

```mysql
select g.id, g.name, s.name as size
from goods as g
    right join size as s
where g.category = 'T-Shirt';
```

### E

```mysql
select g.id, g.name, s.name as size
from goods as g
    outter join size as s
where g.category = 'T-Shirt';
```