提交 785557c9 编写于 作者: M Mars Liu

join

上级 f411bfa8
......@@ -2,9 +2,14 @@
"node_id": "mysql-73bfbd883511436c85130b71fa108038",
"keywords": [],
"children": [],
"export": [],
"export": [
"join_self.json"
],
"keywords_must": [
["mysql", "inner join"]
[
"mysql",
"inner join"
]
],
"keywords_forbid": [],
"group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "join_self.md",
"notebook_enable": false,
"exercise_id": "59e9daf6f4444c2988785af0b75dda10"
}
\ No newline at end of file
# 自连接
现有 node 表如下:
```mysql
create table node(
id int primary key auto_increment,
pid int,
content varchar(256)
)
```
现在Joe 想要给出 content 以 `fork-` 开头的所有节点,和它们的子节点,输出 `parent_id, parent_content, child_id, child_content`
他应该怎么做?
## 答案
```mysql
select l.id as parent_id,
l.content as parent_content,
r.id as child_id,
r.content as child_content
from node as l
join node as r on l.id = r.pid
where l.content like 'fork-%';
```
## 选项
### A
```mysql
select l.id as parent_id,
l.content as parent_content,
r.id as child_id,
r.content as child_content
from node as l
right join node as r on l.id = r.pid
where l.content like 'fork-%';
```
### B
```mysql
select l.id as parent_id,
l.content as parent_content,
r.id as child_id,
r.content as child_content
from node as l, node as r
where l.id =(+) r.pid l.content like 'fork-%';
```
### C
```mysql
select l.id as parent_id,
l.content as parent_content,
r.id as child_id,
r.content as child_content
from node as l
cross join node as r on l.id = r.pid
where l.content like 'fork-%';
```
### D
```mysql
select l.id as parent_id,
l.content as parent_content,
r.id as child_id,
r.content as child_content
from node as l
join node as r on l.pid = r.id
where l.content like 'fork-%';
```
\ No newline at end of file
......@@ -2,9 +2,14 @@
"node_id": "mysql-d7a8869fb798498c839c913ce930015d",
"keywords": [],
"children": [],
"export": [],
"export": [
"left_join.json"
],
"keywords_must": [
["mysql", "left join"]
[
"mysql",
"left join"
]
],
"keywords_forbid": [],
"group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "left_join.md",
"notebook_enable": false,
"exercise_id": "67515869f4fa4661ba858305e5c85c72"
}
\ No newline at end of file
# 左连接
现有部门表
```mysql
create table department(
id int primary key auto_increment,
name varchar(256)
)
```
和员工表
```mysql
create table employee(
id int primary key auto_increment,
dept int,
name varchar(256),
post varchar(16)
)
```
Joe 想要列出所有的部门,如果这个部门有部门助理(post 为 `assistant`),则将 stuff 的名字也列出来,那么这个查询应该是:
## 答案
```mysql
select d.id, d.name, e.name as assistant
from department as d
left join employee as e on e.dept = d.id
where e.post = 'assistant'
```
## 选项
### A
```mysql
select d.id, d.name, e.name as assistant
from department as d
cross join employee as e on e.dept = d.id
where e.post = 'assistant'
```
### B
```mysql
select d.id, d.name, e.name as assistant
from department as d
join employee as e on e.dept = d.id
where e.post = 'assistant'
```
### C
```mysql
select d.id, d.name, e.name as assistant
from department as d
cross join employee as e on e.dept = d.id
where e.post = 'assistant'
```
### D
```mysql
select d.id, d.name, e.name as assistant
from employee as e
left join department as d on e.dept = d.id
where e.post = 'assistant'
```
{
"node_id": "mysql-7c2331eea3e84eef9464ad4d7c03e2de",
"keywords": [],
"keywords": ["right join", "右连接"],
"children": [],
"export": [],
"export": [
"right_join.json"
],
"keywords_must": [
["mysql", "right join"]
[
"mysql",
"right join"
]
],
"keywords_forbid": [],
"group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "right_join.md",
"notebook_enable": false,
"exercise_id": "fbfdc54da9d44fbca30f7a293469bf47"
}
\ No newline at end of file
# 右连接
现有部门表
```mysql
create table department(
id int primary key auto_increment,
name varchar(256)
)
```
和员工表
```mysql
create table employee(
id int primary key auto_increment,
dept int,
name varchar(256),
post varchar(16)
)
```
公司经过了一轮调整后,员工信息有些混乱,现在 Joe 要写一个查询,找出部门信息写
错的员工,这些员工所在的部门在 department 表中没有对应记录。
## 答案
```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;
```
\ No newline at end of file
......@@ -2,9 +2,14 @@
"node_id": "mysql-fd27af20b046463eac7b2712885ac018",
"keywords": [],
"children": [],
"export": [],
"export": [
"cross_join.json"
],
"keywords_must": [
["mysql", "cross join"]
[
"mysql",
"cross join"
]
],
"keywords_forbid": [],
"group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "right_join.md",
"notebook_enable": false,
"exercise_id": "3d9153bd05ed41379bcb13eaadc41ef0"
}
\ No newline at end of file
# 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)
)
```
中,那么这个查询应该是:
## 答案
```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';
```
# 延迟复制
`主从复制`一节里,Joe实现了一个点对点的主从复制架构,其中standby 是 trade的从库,现在,
`主从复制`一节里,Joe实现了一个点对点的主从复制架构,其中 standby 是 trade 的从库,现在,
Joe 要添加一个名为 backup 的新的复制节点,这个节点的同步进度要比 trade 晚半小时。他应该怎么做?
## 答案
......
{
"node_id": "mysql-4381bed0477f412e80f0509b0e2bb3f9",
"keywords": [],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
{
"node_id": "mysql-c140ce4a6f0a4557a3a5ce0b471fd6da",
"keywords": [],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
{
"node_id": "mysql-6e063c40f1ba485288eba2376097b79c",
"keywords": [],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
{
"node_id": "mysql-626f1ca763b344558b3a5eefdb4885a2",
"keywords": [],
"keywords": ["pt-query-digest", "优化"],
"children": [],
"export": [],
"keywords_must": [],
......
{
"node_id": "mysql-66fc4566eaf34994b072ca83bf79ceb4",
"keywords": ["performance", "优化", "group by"],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
......@@ -2448,50 +2448,6 @@
"node_id": "mysql-310130dd922d4e2b832891cc0e157cbf",
"keywords": [],
"children": [
{
" 嵌套查询": {
"node_id": "mysql-4381bed0477f412e80f0509b0e2bb3f9",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
},
{
"ORDER BY 优化": {
"node_id": "mysql-c140ce4a6f0a4557a3a5ce0b471fd6da",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
},
{
"分页查询优化": {
"node_id": "mysql-6e063c40f1ba485288eba2376097b79c",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
},
{
"GROUP BY优化": {
"node_id": "mysql-66fc4566eaf34994b072ca83bf79ceb4",
"keywords": [
"performance",
"优化",
"group by"
],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
},
{
" SHOW STATUS": {
"node_id": "mysql-3574b2e5c9ca475789d9d582d7726906",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册