Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_mysql
提交
ba97f6ea
S
skill_tree_mysql
项目概览
CSDN 技术社区
/
skill_tree_mysql
通知
21
Star
0
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_mysql
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
ba97f6ea
编写于
9月 13, 2022
作者:
M
Mars Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修正左连接题目错误;代码排版
上级
2778a741
变更
66
展开全部
隐藏空白更改
内联
并排
Showing
66 changed file
with
1071 addition
and
645 deletion
+1071
-645
data/1.MySQL初阶/2.安装和登录/2.登录/prepare.md
data/1.MySQL初阶/2.安装和登录/2.登录/prepare.md
+24
-7
data/1.MySQL初阶/5.数据修改/1.插入/config.json
data/1.MySQL初阶/5.数据修改/1.插入/config.json
+8
-2
data/1.MySQL初阶/5.数据修改/1.插入/insert.md
data/1.MySQL初阶/5.数据修改/1.插入/insert.md
+82
-10
data/1.MySQL初阶/5.数据修改/2.修改/update.md
data/1.MySQL初阶/5.数据修改/2.修改/update.md
+10
-4
data/1.MySQL初阶/5.数据修改/3.删除/delete.md
data/1.MySQL初阶/5.数据修改/3.删除/delete.md
+16
-16
data/1.MySQL初阶/6.数据查询/1.SELECT/config.json
data/1.MySQL初阶/6.数据查询/1.SELECT/config.json
+8
-2
data/1.MySQL初阶/6.数据查询/1.SELECT/select.md
data/1.MySQL初阶/6.数据查询/1.SELECT/select.md
+3
-1
data/1.MySQL初阶/6.数据查询/2.WHERE/config.json
data/1.MySQL初阶/6.数据查询/2.WHERE/config.json
+9
-2
data/1.MySQL初阶/6.数据查询/2.WHERE/where.md
data/1.MySQL初阶/6.数据查询/2.WHERE/where.md
+5
-2
data/1.MySQL初阶/6.数据查询/3.运算符/config.json
data/1.MySQL初阶/6.数据查询/3.运算符/config.json
+4
-1
data/1.MySQL初阶/6.数据查询/3.运算符/geo.md
data/1.MySQL初阶/6.数据查询/3.运算符/geo.md
+14
-5
data/2.MySQL中阶/1.数据库组成/3.存储过程和函数/create_procedure.md
data/2.MySQL中阶/1.数据库组成/3.存储过程和函数/create_procedure.md
+9
-3
data/2.MySQL中阶/1.数据库组成/5. 触发器/audit.md
data/2.MySQL中阶/1.数据库组成/5. 触发器/audit.md
+125
-17
data/2.MySQL中阶/11.用户和权限/1.创建普通用户/create_user.md
data/2.MySQL中阶/11.用户和权限/1.创建普通用户/create_user.md
+18
-5
data/2.MySQL中阶/11.用户和权限/1.创建普通用户/create_user_desc.md
data/2.MySQL中阶/11.用户和权限/1.创建普通用户/create_user_desc.md
+0
-11
data/2.MySQL中阶/11.用户和权限/7.限制用户使用资源/limit_action.md
data/2.MySQL中阶/11.用户和权限/7.限制用户使用资源/limit_action.md
+10
-5
data/2.MySQL中阶/2.查询进阶/2. BETWEEN/between.md
data/2.MySQL中阶/2.查询进阶/2. BETWEEN/between.md
+14
-4
data/2.MySQL中阶/2.查询进阶/2. BETWEEN/config.json
data/2.MySQL中阶/2.查询进阶/2. BETWEEN/config.json
+7
-2
data/2.MySQL中阶/2.查询进阶/4.DISTINCT/distinct.md
data/2.MySQL中阶/2.查询进阶/4.DISTINCT/distinct.md
+10
-5
data/2.MySQL中阶/2.查询进阶/5.ORDER BY/order_by.md
data/2.MySQL中阶/2.查询进阶/5.ORDER BY/order_by.md
+18
-6
data/2.MySQL中阶/2.查询进阶/6.UNION/config.json
data/2.MySQL中阶/2.查询进阶/6.UNION/config.json
+7
-2
data/2.MySQL中阶/2.查询进阶/6.UNION/union.md
data/2.MySQL中阶/2.查询进阶/6.UNION/union.md
+4
-4
data/2.MySQL中阶/2.查询进阶/8. 分页查询/paged.md
data/2.MySQL中阶/2.查询进阶/8. 分页查询/paged.md
+20
-4
data/2.MySQL中阶/3.内置函数/5.其它函数/convert.md
data/2.MySQL中阶/3.内置函数/5.其它函数/convert.md
+10
-5
data/2.MySQL中阶/4.聚合和分组/1. 计数/count.md
data/2.MySQL中阶/4.聚合和分组/1. 计数/count.md
+2
-1
data/2.MySQL中阶/4.聚合和分组/2. 求和/sum.md
data/2.MySQL中阶/4.聚合和分组/2. 求和/sum.md
+22
-12
data/2.MySQL中阶/4.聚合和分组/3.最小值/min.md
data/2.MySQL中阶/4.聚合和分组/3.最小值/min.md
+10
-4
data/2.MySQL中阶/4.聚合和分组/5. 平均值/avg.md
data/2.MySQL中阶/4.聚合和分组/5. 平均值/avg.md
+11
-4
data/2.MySQL中阶/4.聚合和分组/5. 平均值/config.json
data/2.MySQL中阶/4.聚合和分组/5. 平均值/config.json
+4
-1
data/2.MySQL中阶/4.聚合和分组/6.HAVING/config.json
data/2.MySQL中阶/4.聚合和分组/6.HAVING/config.json
+4
-1
data/2.MySQL中阶/4.聚合和分组/6.HAVING/having.md
data/2.MySQL中阶/4.聚合和分组/6.HAVING/having.md
+19
-5
data/2.MySQL中阶/5.子查询/1.相关子查询/subquery.md
data/2.MySQL中阶/5.子查询/1.相关子查询/subquery.md
+5
-2
data/2.MySQL中阶/5.子查询/2. ANY/any.md
data/2.MySQL中阶/5.子查询/2. ANY/any.md
+10
-4
data/2.MySQL中阶/5.子查询/3.ALL/all.md
data/2.MySQL中阶/5.子查询/3.ALL/all.md
+20
-5
data/2.MySQL中阶/5.子查询/4.EXISTS/exists.md
data/2.MySQL中阶/5.子查询/4.EXISTS/exists.md
+16
-4
data/2.MySQL中阶/5.子查询/5. IN/in.md
data/2.MySQL中阶/5.子查询/5. IN/in.md
+3
-1
data/2.MySQL中阶/5.子查询/6. 列子查询/in_column.md
data/2.MySQL中阶/5.子查询/6. 列子查询/in_column.md
+16
-15
data/2.MySQL中阶/6.连接查询/2. LEFT JOIN/left_join.md
data/2.MySQL中阶/6.连接查询/2. LEFT JOIN/left_join.md
+5
-2
data/2.MySQL中阶/6.连接查询/3. RIGHT JOIN/right_join.md
data/2.MySQL中阶/6.连接查询/3. RIGHT JOIN/right_join.md
+2
-2
data/2.MySQL中阶/7. 索引/2.创建索引/config.json
data/2.MySQL中阶/7. 索引/2.创建索引/config.json
+4
-1
data/2.MySQL中阶/7. 索引/2.创建索引/create_index.md
data/2.MySQL中阶/7. 索引/2.创建索引/create_index.md
+9
-4
data/2.MySQL中阶/7. 索引/3.删除索引/drop_index.md
data/2.MySQL中阶/7. 索引/3.删除索引/drop_index.md
+8
-4
data/3.MySQL高阶/1.深入索引/1.唯一索引/unique.md
data/3.MySQL高阶/1.深入索引/1.唯一索引/unique.md
+8
-4
data/3.MySQL高阶/1.深入索引/2.全值匹配/config.json
data/3.MySQL高阶/1.深入索引/2.全值匹配/config.json
+7
-1
data/3.MySQL高阶/1.深入索引/2.全值匹配/total_index.md
data/3.MySQL高阶/1.深入索引/2.全值匹配/total_index.md
+8
-3
data/3.MySQL高阶/1.深入索引/3.匹配顺序/match_fields.md
data/3.MySQL高阶/1.深入索引/3.匹配顺序/match_fields.md
+24
-4
data/3.MySQL高阶/1.深入索引/4.组合索引/combinate.md
data/3.MySQL高阶/1.深入索引/4.组合索引/combinate.md
+21
-5
data/3.MySQL高阶/1.深入索引/5.空间索引/geo_index.md
data/3.MySQL高阶/1.深入索引/5.空间索引/geo_index.md
+16
-8
data/3.MySQL高阶/1.深入索引/6.全文索引/full_text_index.md
data/3.MySQL高阶/1.深入索引/6.全文索引/full_text_index.md
+9
-4
data/3.MySQL高阶/1.深入索引/7.隐藏索引/config.json
data/3.MySQL高阶/1.深入索引/7.隐藏索引/config.json
+5
-1
data/3.MySQL高阶/1.深入索引/7.隐藏索引/invisible.md
data/3.MySQL高阶/1.深入索引/7.隐藏索引/invisible.md
+11
-5
data/3.MySQL高阶/1.深入索引/8.函数和表达式索引/daily_payment.md
data/3.MySQL高阶/1.深入索引/8.函数和表达式索引/daily_payment.md
+6
-3
data/3.MySQL高阶/2.SQL高级技巧/1.CTE和递归查询/continuous.md
data/3.MySQL高阶/2.SQL高级技巧/1.CTE和递归查询/continuous.md
+14
-10
data/3.MySQL高阶/2.SQL高级技巧/2.Window Function/salary.md
data/3.MySQL高阶/2.SQL高级技巧/2.Window Function/salary.md
+26
-7
data/3.MySQL高阶/2.SQL高级技巧/3.透视表 /config.json
data/3.MySQL高阶/2.SQL高级技巧/3.透视表 /config.json
+4
-1
data/3.MySQL高阶/2.SQL高级技巧/3.透视表 /pivot.md
data/3.MySQL高阶/2.SQL高级技巧/3.透视表 /pivot.md
+48
-24
data/3.MySQL高阶/2.SQL高级技巧/4.Double Not Exists/DoubleNotExists.md
....MySQL高阶/2.SQL高级技巧/4.Double Not Exists/DoubleNotExists.md
+16
-13
data/3.MySQL高阶/2.SQL高级技巧/4.Double Not Exists/config.json
data/3.MySQL高阶/2.SQL高级技巧/4.Double Not Exists/config.json
+4
-1
data/3.MySQL高阶/2.SQL高级技巧/5.写入和冲突/score.md
data/3.MySQL高阶/2.SQL高级技巧/5.写入和冲突/score.md
+2
-1
data/3.MySQL高阶/3.过程化编程/5. 游标/cursor.md
data/3.MySQL高阶/3.过程化编程/5. 游标/cursor.md
+18
-4
data/3.MySQL高阶/3.过程化编程/5. 游标/cursor_desc.md
data/3.MySQL高阶/3.过程化编程/5. 游标/cursor_desc.md
+0
-9
data/3.MySQL高阶/4.设计优化/1.生成列/generated.md
data/3.MySQL高阶/4.设计优化/1.生成列/generated.md
+8
-4
data/3.MySQL高阶/4.设计优化/4.增加中间表/daily_payment2.md
data/3.MySQL高阶/4.设计优化/4.增加中间表/daily_payment2.md
+22
-9
data/3.MySQL高阶/5.运维与架构/6.复制过滤器/filter.md
data/3.MySQL高阶/5.运维与架构/6.复制过滤器/filter.md
+10
-5
data/3.MySQL高阶/6.查询优化/3. EXPLAIN/explain.md
data/3.MySQL高阶/6.查询优化/3. EXPLAIN/explain.md
+32
-28
data/tree.json
data/tree.json
+147
-294
未找到文件。
data/1.MySQL初阶/2.安装和登录/2.登录/prepare.md
浏览文件 @
ba97f6ea
...
...
@@ -25,8 +25,13 @@ mysql mysql
```
创建数据库用户 joe 并授权:
```
sql
create
user
'joe'
@
'localhost'
identified
by
'joe'
;
grant
all
privileges
on
*
.
*
to
`joe`
@
`localhost`
;
create
user
'joe'
@
'localhost'
identified
by
'joe'
;
grant
all
privileges
on
*
.
*
to
`joe`
@
`localhost`
;
flush
privileges
;
```
...
...
@@ -41,7 +46,9 @@ mysql mysql
```
为 joe 授权
```
sql
grant
all
privileges
on
*
.
*
to
joe
;
grant
all
privileges
on
*
.
*
to
joe
;
flush
privileges
;
```
...
...
@@ -55,7 +62,9 @@ mysql mysql
为 joe 授权
```
sql
grant
all
privileges
on
*
.
*
to
joe
;
grant
all
privileges
on
*
.
*
to
joe
;
flush
privileges
;
```
...
...
@@ -68,7 +77,10 @@ mysql mysql
创建数据库用户 joe
```
sql
create
user
'joe'
@
'localhost'
identified
by
'joe'
;
create
user
'joe'
@
'localhost'
identified
by
'joe'
;
flush
privileges
;
```
...
...
@@ -81,7 +93,12 @@ mysql mysql
```
创建数据库用户 joe 并授权:
```
sql
create
user
'joe'
@
'%'
identified
by
'joe'
;
grant
all
privileges
on
*
.
*
to
joe
;
create
user
'joe'
@
'%'
identified
by
'joe'
;
grant
all
privileges
on
*
.
*
to
joe
;
flush
privileges
;
```
data/1.MySQL初阶/5.数据修改/1.插入/config.json
浏览文件 @
ba97f6ea
{
"node_id"
:
"mysql-fcca525ab0f04f16834ded9b2b3f38a4"
,
"keywords"
:
[
"insert"
,
"插入"
],
"keywords"
:
[
"insert"
,
"插入"
],
"children"
:
[],
"export"
:
[
"insert.json"
],
"keywords_must"
:
[
[
"mysql"
,
"插入"
]
[
"mysql"
,
"插入"
]
],
"keywords_forbid"
:
[],
"group"
:
1
...
...
data/1.MySQL初阶/5.数据修改/1.插入/insert.md
浏览文件 @
ba97f6ea
...
...
@@ -26,8 +26,24 @@ create unique index idx_book_isbn on book(isbn);
## 答案
```
sql
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
'a book title'
,
25
.
4
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
'a other book title'
,
25
.
4
,
'yy-yyyy-xxxx'
,
'2019-12-1'
;
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
'a book title'
,
25
.
4
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
'a other book title'
,
25
.
4
,
'yy-yyyy-xxxx'
,
'2019-12-1'
;
```
## 选项
...
...
@@ -35,27 +51,83 @@ insert into book(title, price, isbn, publish_at) select 'a other book title', 25
### 唯一键冲突
```
sql
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
'a book title'
,
25
.
4
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
'a other book title'
,
35
.
4
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
'a book title'
,
25
.
4
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
'a other book title'
,
35
.
4
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
```
### 缺少必要的列
```
sql
insert
into
book
(
price
,
isbn
,
publish_at
)
select
25
.
4
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
price
,
isbn
,
publish_at
)
select
35
.
4
,
'yy-yyyy-xxxx'
,
'2019-12-1'
;
insert
into
book
(
price
,
isbn
,
publish_at
)
select
25
.
4
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
price
,
isbn
,
publish_at
)
select
35
.
4
,
'yy-yyyy-xxxx'
,
'2019-12-1'
;
```
### 类型错误
```
sql
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
'a book title'
,
'unknown'
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
'a other book title'
,
'unknown'
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
'a book title'
,
'unknown'
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
'a other book title'
,
'unknown'
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
```
### 违反非空约束
```
sql
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
null
,
'unknown'
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
null
,
'unknown'
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
null
,
'unknown'
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
insert
into
book
(
title
,
price
,
isbn
,
publish_at
)
select
null
,
'unknown'
,
'xx-xxxx-xxxx'
,
'2019-12-1'
;
```
data/1.MySQL初阶/5.数据修改/2.修改/update.md
浏览文件 @
ba97f6ea
...
...
@@ -24,7 +24,9 @@ Joe 希望修改销售部(dept 字段为 sale)员工 Dora Muk 的工资,将其
## 答案
```
sql
update
employee
set
salary
=
salary
+
1000
where
dept
=
'sale'
and
name
=
'Dora Muk'
;
update
employee
set
salary
=
salary
+
1000
where
dept
=
'sale'
and
name
=
'Dora Muk'
;
```
## 选项
...
...
@@ -32,19 +34,23 @@ update employee set salary = salary + 1000 where dept = 'sale' and name = 'Dora
### 过滤条件不严谨
```
sql
update
employee
set
salary
=
salary
+
1000
where
name
=
'Dora Muk'
;
update
employee
set
salary
=
salary
+
1000
where
name
=
'Dora Muk'
;
```
### 缺少过滤条件
```
sql
update
employee
set
salary
=
salary
+
1000
;
update
employee
set
salary
=
salary
+
1000
;
```
### 错误的赋值语句
```
sql
update
employee
set
salary
+=
1000
;
update
employee
set
salary
+=
1000
;
```
data/1.MySQL初阶/5.数据修改/3.删除/delete.md
浏览文件 @
ba97f6ea
...
...
@@ -4,14 +4,14 @@
```
sql
create
table
orders
(
id
int
primary
key
auto_increment
,
item_id
int
,
amount
int
,
unit_price
decimal
(
12
,
4
),
price
decimal
(
12
,
4
),
description
varchar
(
2000
),
ts
timestamp
default
now
(),
deal
bool
default
false
id
int
primary
key
auto_increment
,
item_id
int
,
amount
int
,
unit_price
decimal
(
12
,
4
),
price
decimal
(
12
,
4
),
description
varchar
(
2000
),
ts
timestamp
default
now
(),
deal
bool
default
false
);
```
...
...
@@ -60,14 +60,14 @@ from orders;
```
sql
drop
table
orders
;
create
table
if
not
exists
orders
(
id
int
primary
key
auto_increment
,
item_id
int
,
amount
int
,
unit_price
decimal
(
12
,
4
),
price
decimal
(
12
,
4
),
description
varchar
(
2000
),
ts
timestamp
default
now
(),
deal
bool
default
false
id
int
primary
key
auto_increment
,
item_id
int
,
amount
int
,
unit_price
decimal
(
12
,
4
),
price
decimal
(
12
,
4
),
description
varchar
(
2000
),
ts
timestamp
default
now
(),
deal
bool
default
false
);
```
...
...
data/1.MySQL初阶/6.数据查询/1.SELECT/config.json
浏览文件 @
ba97f6ea
{
"node_id"
:
"mysql-f5527eae7f3148108c92ff99a6d4ed4a"
,
"keywords"
:
[
"查询"
,
"select"
],
"keywords"
:
[
"查询"
,
"select"
],
"children"
:
[],
"export"
:
[
"select.json"
],
"keywords_must"
:
[
[
"mysql"
,
"select"
]
[
"mysql"
,
"select"
]
],
"keywords_forbid"
:
[],
"group"
:
1
...
...
data/1.MySQL初阶/6.数据查询/1.SELECT/select.md
浏览文件 @
ba97f6ea
...
...
@@ -39,7 +39,9 @@ select * from employee where dept = 'hr';
### D
```
sql
select
id
,
name
,
dept
,
salary
from
employee
where
salary
>
10000
;
select
id
,
name
,
dept
,
salary
from
employee
where
salary
>
10000
;
```
### E
...
...
data/1.MySQL初阶/6.数据查询/2.WHERE/config.json
浏览文件 @
ba97f6ea
{
"node_id"
:
"mysql-91cc9c73e58945d3ba654370a057a1c7"
,
"keywords"
:
[
"查询"
,
"select"
,
"where"
],
"keywords"
:
[
"查询"
,
"select"
,
"where"
],
"children"
:
[],
"export"
:
[
"where.json"
],
"keywords_must"
:
[
[
"mysql"
,
"where"
]
[
"mysql"
,
"where"
]
],
"keywords_forbid"
:
[],
"group"
:
1
...
...
data/1.MySQL初阶/6.数据查询/2.WHERE/where.md
浏览文件 @
ba97f6ea
...
...
@@ -40,7 +40,9 @@ where date(ts) = '2022-05-25'
```
sql
select
id
from
(
select
*
from
orders
where
date
(
ts
)
=
'2022-05-25'
)
as
o
from
(
select
*
from
orders
where
date
(
ts
)
=
'2022-05-25'
)
as
o
where
unit_prise
<
20
;
```
...
...
@@ -58,7 +60,8 @@ where date(ts) = '2022-05-25'
```
sql
select
id
from
orders
if
date
(
ts
)
=
'2022-05-25'
or
unit_prise
<
20
;
if
date
(
ts
)
=
'2022-05-25'
or
unit_prise
<
20
;
```
### D
...
...
data/1.MySQL初阶/6.数据查询/3.运算符/config.json
浏览文件 @
ba97f6ea
...
...
@@ -9,7 +9,10 @@
"geo.json"
],
"keywords_must"
:
[
[
"mysql"
,
"运算符"
]
[
"mysql"
,
"运算符"
]
],
"keywords_forbid"
:
[],
"group"
:
1
...
...
data/1.MySQL初阶/6.数据查询/3.运算符/geo.md
浏览文件 @
ba97f6ea
...
...
@@ -22,7 +22,8 @@ create table points(
## 答案
```
sql
select
id
,
sqrt
(
x
^
2
+
y
^
2
)
from
points
;
select
id
,
sqrt
(
x
^
2
+
y
^
2
)
from
points
;
```
## 选项
...
...
@@ -30,23 +31,30 @@ select id, sqrt(x^2 + y^2) from points;
### A
```
sql
select
sqrt
(
vx
+
vy
)
from
(
select
x
^
2
as
vx
,
y
^
2
as
vy
from
points
)
as
t
;
select
sqrt
(
vx
+
vy
)
from
(
select
x
^
2
as
vx
,
y
^
2
as
vy
from
points
)
as
t
;
```
### B
```
sql
select
sqrt
(
vx
+
vy
)
from
points
where
x
^
2
as
vx
,
y
^
2
as
vy
;
select
sqrt
(
vx
+
vy
)
from
points
where
x
^
2
as
vx
,
y
^
2
as
vy
;
```
### C
```
sql
select
id
+
sqrt
(
x
^
2
+
y
^
2
)
from
points
;
select
id
+
sqrt
(
x
^
2
+
y
^
2
)
from
points
;
```
### D
```
sql
select
id
||
sqrt
(
x
^
2
+
y
^
2
)
from
points
;
select
id
||
sqrt
(
x
^
2
+
y
^
2
)
from
points
;
```
\ No newline at end of file
data/2.MySQL中阶/1.数据库组成/3.存储过程和函数/create_procedure.md
浏览文件 @
ba97f6ea
...
...
@@ -14,7 +14,10 @@ Joe 希望这个计算更紧凑一些,在已经有 individual_income_tax 的
## 答案
```
sql
create
procedure
sp_idt
(
in
salary
decimal
(
12
,
4
),
out
tax
decimal
(
12
,
4
),
out
take_home
decimal
(
12
,
4
))
create
procedure
sp_idt
(
in
salary
decimal
(
12
,
4
),
out
tax
decimal
(
12
,
4
),
out
take_home
decimal
(
12
,
4
))
begin
set
tax
=
individual_income_tax
(
salary
);
set
take_home
=
salary
-
tax
;
...
...
@@ -26,7 +29,9 @@ end;
### A
```
sql
create
procedure
sp_idt
(
salary
decimal
(
12
,
4
))
returns
(
tax
decimal
(
12
,
4
),
take_home
decimal
(
12
,
4
))
create
procedure
sp_idt
(
salary
decimal
(
12
,
4
))
returns
(
tax
decimal
(
12
,
4
),
take_home
decimal
(
12
,
4
))
begin
set
tax
=
individual_income_tax
(
salary
);
set
take_home
=
salary
-
tax
;
...
...
@@ -37,7 +42,8 @@ end;
```
sql
create
procedure
sp_idt
(
salary
decimal
(
12
,
4
))
returns
(
decimal
(
12
,
4
),
decimal
(
12
,
4
))
create
procedure
sp_idt
(
salary
decimal
(
12
,
4
))
returns
(
decimal
(
12
,
4
),
decimal
(
12
,
4
))
begin
declare
tax
,
take_home
decimal
(
12
,
4
);
set
tax
=
individual_income_tax
(
salary
);
...
...
data/2.MySQL中阶/1.数据库组成/5. 触发器/audit.md
浏览文件 @
ba97f6ea
...
...
@@ -45,13 +45,44 @@ create table orders_log
## 答案
```
sql
create
trigger
in_orders
after
insert
on
orders
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
NEW
.
id
,
NEW
.
item_id
,
NEW
.
amount
,
NEW
.
unit_price
,
NEW
.
total
,
NEW
.
description
,
NEW
.
ts
,
'in'
);
create
trigger
out_orders
after
delete
on
orders
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
OLD
.
id
,
OLD
.
item_id
,
OLD
.
amount
,
OLD
.
unit_price
,
OLD
.
total
,
OLD
.
description
,
OLD
.
ts
,
'out'
);
create
trigger
in_orders
after
insert
on
orders
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
NEW
.
id
,
NEW
.
item_id
,
NEW
.
amount
,
NEW
.
unit_price
,
NEW
.
total
,
NEW
.
description
,
NEW
.
ts
,
'in'
);
create
trigger
out_orders
after
delete
on
orders
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
OLD
.
id
,
OLD
.
item_id
,
OLD
.
amount
,
OLD
.
unit_price
,
OLD
.
total
,
OLD
.
description
,
OLD
.
ts
,
'out'
);
```
## 选项
...
...
@@ -60,8 +91,23 @@ create trigger out_orders after delete on orders
```
sql
create
trigger
in_orders
after
insert
on
orders
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
NEW
.
id
,
NEW
.
item_id
,
NEW
.
amount
,
NEW
.
unit_price
,
NEW
.
total
,
NEW
.
description
,
NEW
.
ts
,
'in'
);
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
NEW
.
id
,
NEW
.
item_id
,
NEW
.
amount
,
NEW
.
unit_price
,
NEW
.
total
,
NEW
.
description
,
NEW
.
ts
,
'in'
);
```
...
...
@@ -70,8 +116,23 @@ create trigger in_orders after insert on orders
```
sql
create
trigger
out_orders
after
delete
on
orders
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
OLD
.
id
,
OLD
.
item_id
,
OLD
.
amount
,
OLD
.
unit_price
,
OLD
.
total
,
OLD
.
description
,
OLD
.
ts
,
'out'
);
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
OLD
.
id
,
OLD
.
item_id
,
OLD
.
amount
,
OLD
.
unit_price
,
OLD
.
total
,
OLD
.
description
,
OLD
.
ts
,
'out'
);
```
...
...
@@ -79,17 +140,63 @@ create trigger out_orders after delete on orders
```
sql
create
trigger
in_orders
after
insert
on
orders
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
NEW
.
id
,
NEW
.
item_id
,
NEW
.
amount
,
NEW
.
unit_price
,
NEW
.
total
,
NEW
.
description
,
NEW
.
ts
,
'in'
);
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
NEW
.
id
,
NEW
.
item_id
,
NEW
.
amount
,
NEW
.
unit_price
,
NEW
.
total
,
NEW
.
description
,
NEW
.
ts
,
'in'
);
after
delete
on
orders
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
OLD
.
id
,
OLD
.
item_id
,
OLD
.
amount
,
OLD
.
unit_price
,
OLD
.
total
,
OLD
.
description
,
OLD
.
ts
,
'out'
);
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
OLD
.
id
,
OLD
.
item_id
,
OLD
.
amount
,
OLD
.
unit_price
,
OLD
.
total
,
OLD
.
description
,
OLD
.
ts
,
'out'
);
```
### D
```
sql
create
trigger
in_orders
after
insert
,
after
delete
on
orders
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
NEW
.
id
,
NEW
.
item_id
,
NEW
.
amount
,
NEW
.
unit_price
,
NEW
.
total
,
NEW
.
description
,
NEW
.
ts
,
'in'
);
for
each
row
insert
into
orders_log
(
id
,
item_id
,
amount
,
unit_price
,
total
,
description
,
ts
,
direction
)
values
(
NEW
.
id
,
NEW
.
item_id
,
NEW
.
amount
,
NEW
.
unit_price
,
NEW
.
total
,
NEW
.
description
,
NEW
.
ts
,
'in'
);
```
\ No newline at end of file
data/2.MySQL中阶/11.用户和权限/1.创建普通用户/create_user.md
浏览文件 @
ba97f6ea
...
...
@@ -13,8 +13,11 @@ Joe 要给数据组的 John 创建一个用户,他希望John 能够从 `192.16
## 答案
```
sql
create
user
'john'
@
'192.168.7.42'
identified
by
'goods123'
password
expire
;
create
user
'john'
@
'192.168.7.42'
identified
by
'goods123'
password
expire
;
grant
select
on
goods
.
*
to
'john'
@
'192.168.7.42'
;
flush
privileges
;
```
...
...
@@ -23,22 +26,31 @@ flush privileges;
### A
```
sql
create
user
'john'
@
'192.168.7.42'
identified
by
'goods123'
;
create
user
'john'
@
'192.168.7.42'
identified
by
'goods123'
;
grant
select
on
goods
.
*
to
'john'
@
'192.168.7.42'
;
flush
privileges
;
```
### B
```
sql
create
user
'john'
@
'192.168.7.42'
identified
by
'goods123'
;
grant
usage
on
goods
.
*
to
'john'
@
'192.168.7.42'
;
create
user
'john'
@
'192.168.7.42'
identified
by
'goods123'
;
grant
usage
on
goods
.
*
to
'john'
@
'192.168.7.42'
;
flush
privileges
;
```
### C
```
sql
create
user
'john'
@
'192.168.7.42'
identified
by
'goods123'
on
database
goods
;
create
user
'john'
@
'192.168.7.42'
identified
by
'goods123'
on
database
goods
;
flush
privileges
;
```
\ No newline at end of file
data/2.MySQL中阶/11.用户和权限/1.创建普通用户/create_user_desc.md
浏览文件 @
ba97f6ea
...
...
@@ -22,31 +22,21 @@ IDENTIFIED BY '@Binghe123456';
### A
```
可以使用GRANT来创建用户
```
### B
```
可以使用CREATE USER语句来创建用户
```
### C
```
CREATE USER 'binghe'@'localhost';
创建用户名为binghe的MySQL用户,其主机名为localhost。
```
### D
```
使用GRANT语句创建用户,不仅可以添加用户,而且还能为用户赋予相应的权限。
```
### E
```
使用CREATE USER语句创建用户时,只是在mysql数据库下的user数据表中添加了一条记录,并没有为用户授权。
```
\ No newline at end of file
data/2.MySQL中阶/11.用户和权限/7.限制用户使用资源/limit_action.md
浏览文件 @
ba97f6ea
...
...
@@ -12,7 +12,8 @@ Joe 需要限制数据分析组(role analysis)的用户, 每小时查询次
## 答案
```
sql
alter
user
analysis
with
MAX_QUERIES_PER_HOUR
10000
;
alter
user
analysis
with
MAX_QUERIES_PER_HOUR
10000
;
```
## 选项
...
...
@@ -20,24 +21,28 @@ alter user analysis with MAX_QUERIES_PER_HOUR 10000;
### A
```
sql
set
user
analysis
with
MAX_QUERIES_PER_HOUR
10000
;
set
user
analysis
with
MAX_QUERIES_PER_HOUR
10000
;
```
### B
```
sql
alter
role
analysis
with
MAX_QUERIES_PER_HOUR
10000
;
alter
role
analysis
with
MAX_QUERIES_PER_HOUR
10000
;
```
### C
```
sql
alter
role
analysis
with
MAX_QUERIES_PER_HOUR
=
10000
;
alter
role
analysis
with
MAX_QUERIES_PER_HOUR
=
10000
;
```
### D
```
sql
alter
user
analysis
set
MAX_QUERIES_PER_HOUR
10000
;
alter
user
analysis
set
MAX_QUERIES_PER_HOUR
10000
;
```
data/2.MySQL中阶/2.查询进阶/2. BETWEEN/between.md
浏览文件 @
ba97f6ea
...
...
@@ -24,7 +24,10 @@ create table goods(
## 答案
```
sql
SELECT
*
FROM
goods
HAVING
price
BETWEEN
1000
AND
2000
;
SELECT
*
FROM
goods
HAVING
price
BETWEEN
1000
AND
2000
;
```
...
...
@@ -33,18 +36,25 @@ SELECT * FROM goods HAVING price BETWEEN 1000 AND 2000;
### A
```
sql
SELECT
*
FROM
goods
WHERE
price
BETWEEN
1000
AND
2000
;
SELECT
*
FROM
goods
WHERE
price
BETWEEN
1000
AND
2000
;
```
### B
```
sql
SELECT
*
FROM
goods
WHERE
price
>=
1000
AND
price
<=
2000
;
SELECT
*
FROM
goods
WHERE
price
>=
1000
AND
price
<=
2000
;
```
### C
```
sql
SELECT
*
FROM
goods
WHERE
not
(
price
<
1000
or
price
>
2000
);
SELECT
*
FROM
goods
WHERE
not
(
price
<
1000
or
price
>
2000
);
```
data/2.MySQL中阶/2.查询进阶/2. BETWEEN/config.json
浏览文件 @
ba97f6ea
{
"node_id"
:
"mysql-2296dbe96d584a52bd28a3ad5f655518"
,
"keywords"
:
[
"where"
],
"keywords"
:
[
"where"
],
"children"
:
[],
"export"
:
[
"between.json"
],
"keywords_must"
:
[
[
"mysql"
,
"between"
]
[
"mysql"
,
"between"
]
],
"keywords_forbid"
:
[],
"group"
:
1
...
...
data/2.MySQL中阶/2.查询进阶/4.DISTINCT/distinct.md
浏览文件 @
ba97f6ea
...
...
@@ -26,7 +26,8 @@ create table goods(
## 答案
```
sql
select
count
(
distinct
price
)
from
goods
;
select
count
(
distinct
price
)
from
goods
;
```
## 选项
...
...
@@ -34,23 +35,27 @@ select count(distinct price) from goods;
### A
```
sql
select
count
(
distinct
*
)
from
goods
;
select
count
(
distinct
*
)
from
goods
;
```
### B
```
sql
select
distinct
count
(
price
)
from
goods
;
select
distinct
count
(
price
)
from
goods
;
```
### C
```
sql
select
count
(
price
)
from
goods
;
select
count
(
price
)
from
goods
;
```
### D
```
sql
select
distinct
price
from
goods
;
select
distinct
price
from
goods
;
```
data/2.MySQL中阶/2.查询进阶/5.ORDER BY/order_by.md
浏览文件 @
ba97f6ea
...
...
@@ -24,7 +24,9 @@ create table employee
## 答案
```
sql
select
id
,
name
,
dept
,
salary
from
employee
order
by
dept
,
salary
desc
;
select
id
,
name
,
dept
,
salary
from
employee
order
by
dept
,
salary
desc
;
```
## 选项
...
...
@@ -32,28 +34,38 @@ select id, name, dept, salary from employee order by dept, salary desc;
### A
```
sql
select
id
,
name
,
dept
,
salary
from
employee
order
by
dept
,
salary
;
select
id
,
name
,
dept
,
salary
from
employee
order
by
dept
,
salary
;
```
### B
```
sql
select
id
,
name
,
dept
,
salary
from
employee
order
by
dept
desc
,
salary
desc
;
select
id
,
name
,
dept
,
salary
from
employee
order
by
dept
desc
,
salary
desc
;
```
### C
```
sql
select
id
,
name
,
dept
,
salary
from
employee
order
by
dept
and
salary
desc
;
select
id
,
name
,
dept
,
salary
from
employee
order
by
dept
and
salary
desc
;
```
### D
```
sql
select
id
,
name
,
dept
,
salary
from
employee
order
by
dept
,
salary
,
id
,
name
;
select
id
,
name
,
dept
,
salary
from
employee
order
by
dept
,
salary
,
id
,
name
;
```
### E
```
sql
select
id
,
name
,
dept
,
salary
from
employee
order
by
dept
,
salary
desc
;
select
id
,
name
,
dept
,
salary
from
employee
order
by
dept
,
salary
desc
;
```
data/2.MySQL中阶/2.查询进阶/6.UNION/config.json
浏览文件 @
ba97f6ea
{
"node_id"
:
"mysql-b57b6c08f5f240c6a997284e4448f088"
,
"keywords"
:
[
"union"
],
"keywords"
:
[
"union"
],
"children"
:
[],
"export"
:
[
"union.json"
],
"keywords_must"
:
[
[
"mysql"
,
"union"
]
[
"mysql"
,
"union"
]
],
"keywords_forbid"
:
[],
"group"
:
1
...
...
data/2.MySQL中阶/2.查询进阶/6.UNION/union.md
浏览文件 @
ba97f6ea
...
...
@@ -12,10 +12,10 @@ create table employee(
);
create
table
customer
(
id
int
primary
key
auto_increment
,
name
varchar
(
256
),
address
varchar
(
1024
),
level
int
id
int
primary
key
auto_increment
,
name
varchar
(
256
),
address
varchar
(
1024
),
level
int
-- ignore more
)
...
...
data/2.MySQL中阶/2.查询进阶/8. 分页查询/paged.md
浏览文件 @
ba97f6ea
...
...
@@ -25,7 +25,11 @@ create table orders
## 答案
```
sql
select
id
,
product_id
,
order_date
,
quantity
,
customer_id
select
id
,
product_id
,
order_date
,
quantity
,
customer_id
from
orders
where
date
=
$
1
offset
$
2
limit
100
;
...
...
@@ -36,7 +40,11 @@ offset $2 limit 100;
### 缺少 limit
```
sql
select
id
,
product_id
,
order_date
,
quantity
,
customer_id
select
id
,
product_id
,
order_date
,
quantity
,
customer_id
from
orders
where
date
=
$
1
offset
$
2
;
...
...
@@ -45,7 +53,11 @@ offset $2;
### 缺少 offset
```
sql
select
id
,
product_id
,
order_date
,
quantity
,
customer_id
select
id
,
product_id
,
order_date
,
quantity
,
customer_id
from
orders
where
date
=
$
1
;
```
...
...
@@ -53,7 +65,11 @@ where date = $1;
### 结构不对
```
sql
select
id
,
product_id
,
order_date
,
quantity
,
customer_id
select
id
,
product_id
,
order_date
,
quantity
,
customer_id
from
orders
where
date
=
$
1
and
offset
$
2
and
limit
100
;
...
...
data/2.MySQL中阶/3.内置函数/5.其它函数/convert.md
浏览文件 @
ba97f6ea
...
...
@@ -18,7 +18,8 @@ select name from goods;
## 答案
```
sql
select
convert
(
name
using
'gb18030'
)
from
goods
;
select
convert
(
name
using
'gb18030'
)
from
goods
;
```
## 选项
...
...
@@ -27,27 +28,31 @@ select convert(name using 'gb18030') from goods;
### A
```
sql
select
str
(
name
,
'gb18303'
)
from
goods
;
select
str
(
name
,
'gb18303'
)
from
goods
;
```
### B
```
sql
select
encode
(
decode
(
name
,
'utf8mb4'
),
'gb18303'
)
from
goods
;
select
encode
(
decode
(
name
,
'utf8mb4'
),
'gb18303'
)
from
goods
;
```
### C
```
sql
select
convert
(
name
from
'utf8mb4'
to
'gb18303'
)
from
goods
;
select
convert
(
name
from
'utf8mb4'
to
'gb18303'
)
from
goods
;
```
### D
```
sql
select
convert
(
name
to
'gb18303'
)
from
goods
;
select
convert
(
name
to
'gb18303'
)
from
goods
;
```
### E
...
...
data/2.MySQL中阶/4.聚合和分组/1. 计数/count.md
浏览文件 @
ba97f6ea
...
...
@@ -17,7 +17,8 @@ mysql> select * from items;
当他执行
```
sql
select
count
(
*
),
count
(
item
)
from
items
;
select
count
(
*
),
count
(
item
)
from
items
;
```
会得到什么结果?
...
...
data/2.MySQL中阶/4.聚合和分组/2. 求和/sum.md
浏览文件 @
ba97f6ea
...
...
@@ -4,14 +4,14 @@ Joe 想要得到 orders 表
```
sql
create
table
orders
(
id
int
primary
key
auto_increment
,
item_id
int
,
amount
int
,
unit_price
decimal
(
12
,
4
),
total
decimal
(
12
,
4
),
description
varchar
(
2000
),
ts
timestamp
default
now
(),
deal
bool
default
false
id
int
primary
key
auto_increment
,
item_id
int
,
amount
int
,
unit_price
decimal
(
12
,
4
),
total
decimal
(
12
,
4
),
description
varchar
(
2000
),
ts
timestamp
default
now
(),
deal
bool
default
false
);
```
...
...
@@ -27,7 +27,9 @@ create table orders (
## 答案
```
sql
select
sum
(
total
)
from
orders
where
deal
and
unit_price
>
1000
;
select
sum
(
total
)
from
orders
where
deal
and
unit_price
>
1000
;
```
## 选项
...
...
@@ -35,17 +37,24 @@ select sum(total) from orders where deal and unit_price > 1000;
### A
```
sql
select
sum
(
total
)
from
orders
having
deal
and
unit_price
>
1000
;
select
sum
(
total
)
from
orders
having
deal
and
unit_price
>
1000
;
```
### B
```
sql
select
sum
(
total
)
from
orders
group
by
deal
having
unit_price
>
1000
;
select
sum
(
total
)
from
orders
group
by
deal
having
unit_price
>
1000
;
```
### C
```
sql
select
sum
(
total
)
from
orders
order
by
deal
and
unit_price
>
1000
;
select
sum
(
total
)
from
orders
order
by
deal
and
unit_price
>
1000
;
```
\ No newline at end of file
data/2.MySQL中阶/4.聚合和分组/3.最小值/min.md
浏览文件 @
ba97f6ea
...
...
@@ -24,7 +24,9 @@ create table employee
## 答案
```
sql
select
dept
,
min
(
salary
)
from
employee
group
by
dept
;
select
dept
,
min
(
salary
)
from
employee
group
by
dept
;
```
## 选项
...
...
@@ -32,17 +34,20 @@ select dept, min(salary) from employee group by dept;
### A
```
sql
select
dept
,
min
(
salary
)
from
employee
;
select
dept
,
min
(
salary
)
from
employee
;
```
### B
```
sql
select
dept
,
min
(
salary
)
from
employee
order
by
dept
;
select
dept
,
min
(
salary
)
from
employee
order
by
dept
;
```
### C
```
sql
select
dept
,
min
(
total
)
from
employee
;
select
dept
,
min
(
total
)
from
employee
;
```
\ No newline at end of file
data/2.MySQL中阶/4.聚合和分组/5. 平均值/avg.md
浏览文件 @
ba97f6ea
...
...
@@ -24,7 +24,9 @@ create table employee
## 答案
```
sql
select
dept
,
avg
(
salary
)
from
employee
group
by
dept
;
select
dept
,
avg
(
salary
)
from
employee
group
by
dept
;
```
## 选项
...
...
@@ -32,17 +34,22 @@ select dept, avg(salary) from employee group by dept;
### A
```
sql
select
dept
,
avg
(
salary
)
from
employee
;
select
dept
,
avg
(
salary
)
from
employee
;
```
### B
```
sql
select
dept
,
avg
(
dept
)
from
employee
group
by
dept
;
select
dept
,
avg
(
dept
)
from
employee
group
by
dept
;
```
### C
```
sql
select
dept
,
avg
(
salary
)
from
employee
group
by
salary
;
select
dept
,
avg
(
salary
)
from
employee
group
by
salary
;
```
data/2.MySQL中阶/4.聚合和分组/5. 平均值/config.json
浏览文件 @
ba97f6ea
{
"node_id"
:
"mysql-11462fccf9d24d17a372d5c60af90f54"
,
"keywords"
:
[
"avg"
,
"平均值"
],
"keywords"
:
[
"avg"
,
"平均值"
],
"children"
:
[],
"export"
:
[
"avg.json"
...
...
data/2.MySQL中阶/4.聚合和分组/6.HAVING/config.json
浏览文件 @
ba97f6ea
...
...
@@ -6,7 +6,10 @@
"having.json"
],
"keywords_must"
:
[
[
"mysql"
,
"having"
]
[
"mysql"
,
"having"
]
],
"keywords_forbid"
:
[],
"group"
:
1
...
...
data/2.MySQL中阶/4.聚合和分组/6.HAVING/having.md
浏览文件 @
ba97f6ea
...
...
@@ -24,7 +24,10 @@ create table employee
## 答案
```
sql
select
dept
from
employee
group
by
dept
having
sum
(
salary
)
>
100000
;
select
dept
from
employee
group
by
dept
having
sum
(
salary
)
>
100000
;
```
## 选项
...
...
@@ -32,25 +35,36 @@ select dept from employee group by dept having sum(salary) > 100000;
### A
```
sql
select
dept
from
employee
group
by
dept
where
sum
(
salary
)
>
100000
;
select
dept
from
employee
group
by
dept
where
sum
(
salary
)
>
100000
;
```
### B
```
sql
select
dept
from
employee
where
sum
(
salary
)
>
100000
group
by
dept
;
select
dept
from
employee
where
sum
(
salary
)
>
100000
group
by
dept
;
```
### C
```
sql
select
dept
from
employee
where
sum
(
salary
)
>
100000
order
by
dept
;
select
dept
from
employee
where
sum
(
salary
)
>
100000
order
by
dept
;
```
### D
```
sql
select
dept
from
employee
group
by
dept
where
sum
(
salary
)
>
100000
;
select
dept
from
employee
group
by
dept
where
sum
(
salary
)
>
100000
;
```
data/2.MySQL中阶/5.子查询/1.相关子查询/subquery.md
浏览文件 @
ba97f6ea
...
...
@@ -48,7 +48,8 @@ having salary > max(salary)
```
sql
select
l
.
id
,
l
.
name
,
l
.
dept
,
l
.
salary
from
employee
as
l
join
employee
as
r
on
l
.
salary
>
max
(
r
.
salary
)
join
employee
as
r
on
l
.
salary
>
max
(
r
.
salary
)
where
r
.
dept
=
'sale'
group
by
r
.
dept
```
...
...
@@ -58,7 +59,9 @@ group by r.dept
```
sql
select
id
,
name
,
dept
,
salary
from
employee
having
salary
>
(
select
max
(
salary
)
from
employee
where
dept
=
'sale'
)
having
salary
>
(
select
max
(
salary
)
from
employee
where
dept
=
'sale'
)
```
...
...
data/2.MySQL中阶/5.子查询/2. ANY/any.md
浏览文件 @
ba97f6ea
...
...
@@ -25,7 +25,9 @@ create table employee(
```
sql
select
id
,
name
,
dept
,
salary
from
employee
as
o
where
o
.
salary
<
any
(
select
salary
from
employee
as
i
where
i
.
dept
=
o
.
dept
)
where
o
.
salary
<
any
(
select
salary
from
employee
as
i
where
i
.
dept
=
o
.
dept
)
```
## 选项
...
...
@@ -35,7 +37,8 @@ where o.salary < any(select salary from employee as i where i.dept=o.dept)
```
sql
select
id
,
name
,
dept
,
salary
from
employee
as
o
join
employee
as
i
on
o
.
dept
=
i
.
dept
and
o
.
salary
<
i
.
salary
join
employee
as
i
on
o
.
dept
=
i
.
dept
and
o
.
salary
<
i
.
salary
```
### B
...
...
@@ -43,7 +46,8 @@ join employee as i on o.dept = i.dept and o.salary < i.salary
```
sql
select
o
.
id
,
o
.
name
,
o
.
dept
,
o
.
salary
from
employee
as
o
left
join
employee
as
i
on
o
.
dept
=
i
.
dept
and
o
.
salary
<
i
.
salary
left
join
employee
as
i
on
o
.
dept
=
i
.
dept
and
o
.
salary
<
i
.
salary
where
i
.
id
is
null
;
```
...
...
@@ -52,6 +56,7 @@ where i.id is null;
```
sql
select
o
.
id
,
o
.
name
,
o
.
dept
,
o
.
salary
from
employee
as
o
left
join
employee
as
i
on
o
.
dept
=
i
.
dept
and
o
.
salary
<
i
.
salary
left
join
employee
as
i
on
o
.
dept
=
i
.
dept
and
o
.
salary
<
i
.
salary
where
i
.
id
is
not
null
;
```
\ No newline at end of file
data/2.MySQL中阶/5.子查询/3.ALL/all.md
浏览文件 @
ba97f6ea
...
...
@@ -25,7 +25,10 @@ create table employee(
```
sql
select
id
,
name
,
dept
from
employee
as
o
where
'assistant'
!=
all
(
select
post
from
employee
as
i
where
o
.
dept
=
i
.
dept
);
where
'assistant'
!=
all
(
select
post
from
employee
as
i
where
o
.
dept
=
i
.
dept
);
```
## 选项
...
...
@@ -35,7 +38,10 @@ where 'assistant' != all(select post from employee as i where o.dept = i.dept);
```
sql
select
id
,
name
,
dept
from
employee
as
o
where
'assistant'
=
all
(
select
post
from
employee
as
i
where
o
.
dept
=
i
.
dept
);
where
'assistant'
=
all
(
select
post
from
employee
as
i
where
o
.
dept
=
i
.
dept
);
```
### B
...
...
@@ -43,14 +49,19 @@ where 'assistant' = all(select post from employee as i where o.dept = i.dept);
```
sql
select
id
,
name
,
dept
from
employee
as
o
where
'assistant'
!=
all
(
select
post
from
employee
as
i
where
o
.
dept
=
i
.
dept
);
where
'assistant'
!=
all
(
select
post
from
employee
as
i
where
o
.
dept
=
i
.
dept
);
```
### C
```
sql
select
id
,
name
,
dept
from
employee
as
o
where
'assistant'
!=
all
(
select
post
from
employee
as
i
);
where
'assistant'
!=
all
(
select
post
from
employee
as
i
);
```
### D
...
...
@@ -58,5 +69,8 @@ where 'assistant' != all(select post from employee as i);
```
sql
select
id
,
name
,
dept
from
employee
as
o
where
'assistant'
!=
ANY
(
select
post
from
employee
as
i
where
o
.
dept
=
i
.
dept
);
where
'assistant'
!=
ANY
(
select
post
from
employee
as
i
where
o
.
dept
=
i
.
dept
);
```
\ No newline at end of file
data/2.MySQL中阶/5.子查询/4.EXISTS/exists.md
浏览文件 @
ba97f6ea
...
...
@@ -26,7 +26,10 @@ create table employee(
```
sql
select
id
,
name
,
dept
from
employee
as
o
where
not
exists
(
select
*
from
employee
as
i
where
o
.
dept
=
i
.
dept
and
post
=
'assistant'
);
where
not
exists
(
select
*
from
employee
as
i
where
o
.
dept
=
i
.
dept
and
post
=
'assistant'
);
```
## 选项
...
...
@@ -36,14 +39,19 @@ where not exists(select * from employee as i where o.dept = i.dept and post='ass
```
sql
select
id
,
name
,
dept
from
employee
as
o
where
exists
(
select
*
from
employee
as
i
where
o
.
dept
=
i
.
dept
and
post
=
'assistant'
);
where
exists
(
select
*
from
employee
as
i
where
o
.
dept
=
i
.
dept
and
post
=
'assistant'
);
```
### B
```
sql
select
id
,
name
,
dept
from
employee
as
o
where
'assistant'
!=
exists
(
select
post
from
employee
as
i
);
where
'assistant'
!=
exists
(
select
post
from
employee
as
i
);
```
### C
...
...
@@ -51,5 +59,8 @@ where 'assistant' != exists(select post from employee as i);
```
sql
select
id
,
name
,
dept
from
employee
as
o
where
'assistant'
=
not
exists
(
select
post
from
employee
as
i
where
o
.
dept
=
i
.
dept
);
where
'assistant'
=
not
exists
(
select
post
from
employee
as
i
where
o
.
dept
=
i
.
dept
);
```
\ No newline at end of file
data/2.MySQL中阶/5.子查询/5. IN/in.md
浏览文件 @
ba97f6ea
...
...
@@ -43,7 +43,9 @@ where dept in (select 'dev', 'hr');
```
sql
select
id
,
dept
,
name
,
post
from
employee
where
dept
in
(
select
*
from
'dev'
,
'hr'
);
where
dept
in
(
select
*
from
'dev'
,
'hr'
);
```
### C
...
...
data/2.MySQL中阶/5.子查询/6. 列子查询/in_column.md
浏览文件 @
ba97f6ea
...
...
@@ -24,10 +24,10 @@ create table employee
## 答案
```
sql
select
distinct
(
dept
)
as
dept
,
(
select
count
(
*
)
from
employee
as
i
where
i
.
dept
=
o
.
dept
)
as
emp
select
distinct
(
dept
)
as
dept
,
(
select
count
(
*
)
from
employee
as
i
where
i
.
dept
=
o
.
dept
)
as
emp
from
employee
as
o
;
```
...
...
@@ -36,28 +36,28 @@ from employee as o;
### A
```
sql
select
distinct
(
dept
)
as
dept
,
(
select
count
(
*
)
from
employee
as
i
)
as
emp
select
distinct
(
dept
)
as
dept
,
(
select
count
(
*
)
from
employee
as
i
)
as
emp
from
employee
as
o
;
```
### B
```
sql
select
distinct
(
dept
)
as
dept
,
(
select
count
(
*
)
from
employee
where
dept
=
dept
)
as
emp
select
distinct
(
dept
)
as
dept
,
(
select
count
(
*
)
from
employee
where
dept
=
dept
)
as
emp
from
employee
```
### C
```
sql
select
dept
as
dept
,
(
select
count
(
*
)
from
employee
as
i
where
i
.
dept
=
o
.
dept
)
as
emp
select
dept
as
dept
,
(
select
count
(
*
)
from
employee
as
i
where
i
.
dept
=
o
.
dept
)
as
emp
from
employee
as
o
```
\ No newline at end of file
data/2.MySQL中阶/6.连接查询/2. LEFT JOIN/left_join.md
浏览文件 @
ba97f6ea
...
...
@@ -34,8 +34,11 @@ Joe 想要列出所有的部门,如果这个部门有部门助理(post 为 `
```
sql
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'
left
join
(
select
name
,
dept_id
from
employee
where
post
=
'assistant'
)
as
e
on
e
.
dept_id
=
d
.
id
```
## 选项
...
...
data/2.MySQL中阶/6.连接查询/3. RIGHT JOIN/right_join.md
浏览文件 @
ba97f6ea
...
...
@@ -46,7 +46,7 @@ where d.id is null;
```
sql
select
e
.
id
,
e
.
name
,
e
.
dept
from
employee
as
e
right
join
department
as
d
on
d
.
id
=
e
.
dept
right
join
department
as
d
on
d
.
id
=
e
.
dept
where
e
.
id
is
null
;
```
...
...
@@ -55,7 +55,7 @@ where e.id is null;
```
sql
select
e
.
id
,
e
.
name
,
e
.
dept
from
employee
as
e
right
join
department
as
d
on
d
.
id
=
e
.
dept
right
join
department
as
d
on
d
.
id
=
e
.
dept
where
d
.
id
is
null
;
```
...
...
data/2.MySQL中阶/7. 索引/2.创建索引/config.json
浏览文件 @
ba97f6ea
{
"node_id"
:
"mysql-fe65d5c615ad40f8ac056cc654f2d788"
,
"keywords"
:
[
"create index"
,
"创建索引"
],
"keywords"
:
[
"create index"
,
"创建索引"
],
"children"
:
[],
"export"
:
[
"create_index.json"
...
...
data/2.MySQL中阶/7. 索引/2.创建索引/create_index.md
浏览文件 @
ba97f6ea
...
...
@@ -27,7 +27,8 @@ create table goods(
## 答案
```
sql
create
index
idx_goods_category
on
goods
(
category_id
);
create
index
idx_goods_category
on
goods
(
category_id
);
```
## 选项
...
...
@@ -35,17 +36,20 @@ create index idx_goods_category on goods(category_id);
### A
```
sql
create
unique
index
idx_goods_category
on
goods
(
category_id
);
create
unique
index
idx_goods_category
on
goods
(
category_id
);
```
### B
```
sql
create
index
idx_goods_category
on
goods
(
name
);
create
index
idx_goods_category
on
goods
(
name
);
```
### C
```
sql
alter
table
goods
add
unique
index
(
category_id
);
alter
table
goods
add
unique
index
(
category_id
);
```
\ No newline at end of file
data/2.MySQL中阶/7. 索引/3.删除索引/drop_index.md
浏览文件 @
ba97f6ea
...
...
@@ -20,7 +20,8 @@ show index from goods;
查看 goods 表的索引(假设查到是 idx_goods_category),然后执行
```
sql
alter
table
goods
drop
index
idx_goods_category
;
alter
table
goods
drop
index
idx_goods_category
;
```
## 选项
...
...
@@ -36,7 +37,8 @@ show index from goods;
查看 goods 表的索引(假设查到是 idx_goods_category),然后执行
```
sql
alter
table
goods
delete
index
idx_goods_category
;
alter
table
goods
delete
index
idx_goods_category
;
```
### B
...
...
@@ -50,7 +52,8 @@ show index from goods;
查看 goods 表的索引(假设查到是 idx_goods_category),然后执行
```
sql
alter
table
goods
remove
index
idx_goods_category
;
alter
table
goods
remove
index
idx_goods_category
;
```
### C
...
...
@@ -64,7 +67,8 @@ show index from goods;
查看 goods 表的索引(假设查到是 idx_goods_category),然后执行
```
sql
alter
table
goods
drop
idx_goods_category
;
alter
table
goods
drop
idx_goods_category
;
```
...
...
data/3.MySQL高阶/1.深入索引/1.唯一索引/unique.md
浏览文件 @
ba97f6ea
...
...
@@ -25,7 +25,8 @@ Joe 需要确保同一个类型下没有重名的商品,他应该怎么做?
## 答案
```
sql
alter
table
goods
add
unique
index
(
category_id
,
name
);
alter
table
goods
add
unique
index
(
category_id
,
name
);
```
## 选项
...
...
@@ -33,17 +34,20 @@ alter table goods add unique index (category_id, name);
### A
```
sql
alter
table
goods
add
index
(
category_id
,
name
);
alter
table
goods
add
index
(
category_id
,
name
);
```
### B
```
sql
alter
table
goods
add
unique
index
(
category_id
+
name
);
alter
table
goods
add
unique
index
(
category_id
+
name
);
```
### C
```
sql
alter
table
goods
add
unique
index
(
concat
(
category_id
,
name
));
alter
table
goods
add
unique
index
(
concat
(
category_id
,
name
));
```
data/3.MySQL高阶/1.深入索引/2.全值匹配/config.json
浏览文件 @
ba97f6ea
...
...
@@ -7,7 +7,12 @@
"export"
:
[
"total_index.json"
],
"keywords_must"
:
[[
"mysql"
,
"全值匹配"
]],
"keywords_must"
:
[
[
"mysql"
,
"全值匹配"
]
],
"keywords_forbid"
:
[],
"group"
:
2
}
\ No newline at end of file
data/3.MySQL高阶/1.深入索引/2.全值匹配/total_index.md
浏览文件 @
ba97f6ea
...
...
@@ -25,7 +25,8 @@ create table goods(
## 答案
```
sql
alter
table
goods
add
index
(
name
,
price
);
alter
table
goods
add
index
(
name
,
price
);
```
## 选项
...
...
@@ -39,11 +40,14 @@ alter table goods add index (name, price);
建立一个计算字段:
```
sql
alter
table
goods
add
summary
varchar
(
1024
)
generated
always
as
(
concat
(
name
,
'('
,
0
.
5
,
')'
));
alter
table
goods
add
summary
varchar
(
1024
)
generated
always
as
(
concat
(
name
,
'('
,
0
.
5
,
')'
));
```
### C
```
sql
alter
table
goods
add
index
(
concat
(
name
,
price
));
alter
table
goods
add
index
(
concat
(
name
,
price
));
```
\ No newline at end of file
data/3.MySQL高阶/1.深入索引/3.匹配顺序/match_fields.md
浏览文件 @
ba97f6ea
...
...
@@ -29,7 +29,12 @@ Joe 发现有大量查询 `select id, category_id, name, price from goods where
将该查询改写为
```
sql
select
id
,
category_id
,
name
,
price
from
goods
where
category_id
=?
and
name
=?
;
select
id
,
category_id
,
name
,
price
from
goods
where
category_id
=?
and
name
=?
;
```
## 选项
...
...
@@ -39,7 +44,12 @@ select id, category_id, name, price from goods where category_id=? and name=?;
将该查询改写为
```
sql
select
id
,
category_id
,
name
,
price
from
goods
where
category_id
and
name
=
(
?
,
?
);
select
id
,
category_id
,
name
,
price
from
goods
where
category_id
and
name
=
(
?
,
?
);
```
### B
...
...
@@ -47,7 +57,12 @@ select id, category_id, name, price from goods where category_id and name= (?, ?
将该查询改写为
```
sql
select
id
,
category_id
,
name
,
price
from
goods
where
not
(
category_id
!=?
or
name
!=
?
);
select
id
,
category_id
,
name
,
price
from
goods
where
not
(
category_id
!=?
or
name
!=
?
);
```
### C
...
...
@@ -55,6 +70,11 @@ select id, category_id, name, price from goods where not (category_id !=? or nam
将该查询改写为
```
sql
select
id
,
category_id
,
name
,
price
from
goods
where
not
(
category_id
!=?
)
and
not
(
name
!=
?
);
select
id
,
category_id
,
name
,
price
from
goods
where
not
(
category_id
!=?
)
and
not
(
name
!=
?
);
```
data/3.MySQL高阶/1.深入索引/4.组合索引/combinate.md
浏览文件 @
ba97f6ea
...
...
@@ -14,7 +14,19 @@ create table goods(
)
```
现有大量查询
`select id, category_id, name, price, stock from goods where stock=? and category_id=? and name like ?`
,
现有大量查询
```
sql
select
id
,
category_id
,
name
,
price
,
stock
from
goods
where
stock
=?
and
category_id
=?
and
name
like
?
`,
```
Joe 应该如何优化?
<hr/>
...
...
@@ -27,7 +39,8 @@ Joe 应该如何优化?
## 答案
```
sql
alter
table
goods
add
index
(
stock
,
category_id
,
name
);
alter
table
goods
add
index
(
stock
,
category_id
,
name
);
```
## 选项
...
...
@@ -35,17 +48,20 @@ alter table goods add index (stock, category_id, name);
### A
```
sql
alter
table
goods
add
index
(
stock
and
category_id
and
name
);
alter
table
goods
add
index
(
stock
and
category_id
and
name
);
```
### B
```
sql
alter
table
goods
add
index
(
concat
(
stock
,
category_id
,
name
));
alter
table
goods
add
index
(
concat
(
stock
,
category_id
,
name
));
```
### C
```
sql
alter
table
goods
add
index
(
stock
+
category_id
+
name
);
alter
table
goods
add
index
(
stock
+
category_id
+
name
);
```
data/3.MySQL高阶/1.深入索引/5.空间索引/geo_index.md
浏览文件 @
ba97f6ea
...
...
@@ -22,8 +22,10 @@ create table shop (
## 答案
```
sql
alter
table
shop
modify
location
GEOMETRY
not
null
;
alter
table
shop
add
INDEX
geo_index
(
location
);
alter
table
shop
modify
location
GEOMETRY
not
null
;
alter
table
shop
add
INDEX
geo_index
(
location
);
```
## 选项
...
...
@@ -31,25 +33,31 @@ alter table shop add INDEX geo_index(location);
### A
```
sql
alter
table
shop
add
INDEX
geo_index
(
location
);
alter
table
shop
add
INDEX
geo_index
(
location
);
```
### B
```
sql
alter
table
shop
add
INDEX
location
;
alter
table
shop
add
INDEX
location
;
```
### C
```
sql
alter
table
shop
modify
location
GEOMETRY
not
null
;
alter
table
shop
add
INDEX
location
;
alter
table
shop
modify
location
GEOMETRY
not
null
;
alter
table
shop
add
INDEX
location
;
```
### D
```
sql
alter
table
shop
modify
location
GEOMETRY
not
null
;
alter
table
shop
add
INDEX
location
;
alter
table
shop
modify
location
GEOMETRY
not
null
;
alter
table
shop
add
INDEX
location
;
```
data/3.MySQL高阶/1.深入索引/6.全文索引/full_text_index.md
浏览文件 @
ba97f6ea
...
...
@@ -22,7 +22,8 @@ create table shop (
## 答案
```
sql
alter
table
shop
add
fulltext
(
description
);
alter
table
shop
add
fulltext
(
description
);
```
## 选项
...
...
@@ -30,17 +31,20 @@ alter table shop add fulltext(description);
### A
```
sql
alter
table
shop
add
index
fulltext
(
description
);
alter
table
shop
add
index
fulltext
(
description
);
```
### B
```
sql
alter
table
shop
create
fulltext
(
description
);
alter
table
shop
create
fulltext
(
description
);
```
### C
```
sql
alter
table
shop
alter
description
add
fulltext
(
description
);
alter
table
shop
alter
description
add
fulltext
(
description
);
```
\ No newline at end of file
data/3.MySQL高阶/1.深入索引/7.隐藏索引/config.json
浏览文件 @
ba97f6ea
{
"node_id"
:
"mysql-c57e1195b0914cd38798ce029156ec87"
,
"keywords"
:
[
"invisible"
,
"visible"
,
"隐藏索引"
],
"keywords"
:
[
"invisible"
,
"visible"
,
"隐藏索引"
],
"children"
:
[],
"export"
:
[
"invisible.json"
...
...
data/3.MySQL高阶/1.深入索引/7.隐藏索引/invisible.md
浏览文件 @
ba97f6ea
...
...
@@ -28,13 +28,15 @@ Joe 应该怎么做?
先执行
```
sql
alter
table
shop
alter
index
description
invisible
;
alter
table
shop
alter
index
description
invisible
;
```
将索引隐藏,观察确认没有影响后再执行
```
sql
alter
table
shop
drop
index
description
;
alter
table
shop
drop
index
description
;
```
删除。
...
...
@@ -47,7 +49,8 @@ alter table shop drop index description;
先备份 shop 表,然后执行
```
sql
alter
table
shop
drop
index
description
;
alter
table
shop
drop
index
description
;
```
删除,有问题的话从备份文件恢复。
...
...
@@ -60,13 +63,15 @@ alter table shop drop index description;
先执行
```
sql
alter
table
shop
alter
index
description
invisible
;
alter
table
shop
alter
index
description
invisible
;
```
将索引隐藏,确认后再执行
```
sql
alter
table
shop
alter
index
description
visible
;
alter
table
shop
alter
index
description
visible
;
```
恢复索引可见。
\ No newline at end of file
data/3.MySQL高阶/1.深入索引/8.函数和表达式索引/daily_payment.md
浏览文件 @
ba97f6ea
...
...
@@ -40,7 +40,8 @@ mysql> desc payment;
建立表达式索引
```
sql
alter
table
payment
add
index
idx_payment_date
((
date
(
payment_date
)));
alter
table
payment
add
index
idx_payment_date
((
date
(
payment_date
)));
```
## 选项
...
...
@@ -50,7 +51,8 @@ alter table payment add index idx_payment_date((date(payment_date)));
建立视图
```
sql
create
view
view_daily_payment
as
select
date
(
payment_date
)
as
day
,
amount
from
payment
;
create
view
view_daily_payment
as
select
date
(
payment_date
)
as
day
,
amount
from
payment
;
```
然后在视图 view_daily_payment 上执行
...
...
@@ -75,7 +77,8 @@ create index idx_payment_date on payment(payment_date);
建立计算列
```
sql
alter
table
payment
add
day
date
generated
always
as
(
date
(
payment_date
))
stored
;
alter
table
payment
add
day
date
generated
always
as
(
date
(
payment_date
))
stored
;
```
然后使用它改写查询
...
...
data/3.MySQL高阶/2.SQL高级技巧/1.CTE和递归查询/continuous.md
浏览文件 @
ba97f6ea
...
...
@@ -28,13 +28,14 @@ create table orders
## 答案
```
sql
with
recursive
r
(
id
)
as
(
select
id
from
orders
where
id
=
$
1
union
select
d
.
id
from
orders
as
d
join
r
on
d
.
id
=
r
.
id
+
1
)
with
recursive
r
(
id
)
as
(
select
id
from
orders
where
id
=
$
1
union
select
d
.
id
from
orders
as
d
join
r
on
d
.
id
=
r
.
id
+
1
)
select
orders
.
id
,
content
from
orders
join
r
on
orders
.
id
=
r
.
id
;
...
...
@@ -50,8 +51,8 @@ from orders
```
sql
select
data
.
id
,
content
from
orders
join
orders
as
r
on
orders
.
id
=
r
.
id
-
1
from
orders
join
orders
as
r
on
orders
.
id
=
r
.
id
-
1
where
id
=
$
1
;
```
...
...
@@ -60,7 +61,10 @@ where id = $1;
```
sql
select
id
,
content
from
orders
where
id
in
(
select
id
from
orders
where
id
=
id
+
1
);
where
id
in
(
select
id
from
orders
where
id
=
id
+
1
);
```
### D
...
...
data/3.MySQL高阶/2.SQL高级技巧/2.Window Function/salary.md
浏览文件 @
ba97f6ea
...
...
@@ -25,7 +25,13 @@ create table employee
```
sql
select
id
,
name
,
dept
,
salary
from
(
select
id
,
name
,
dept
,
salary
,
rank
()
over
(
partition
by
dept
order
by
salary
desc
)
as
r
from
(
select
id
,
name
,
dept
,
salary
,
rank
()
over
(
partition
by
dept
order
by
salary
desc
)
as
r
from
employee
)
as
t
where
r
<=
5
;
```
...
...
@@ -58,16 +64,23 @@ where count(r.id) <= 5;
```
sql
select
l
.
id
,
l
.
name
,
l
.
dept
,
l
.
salary
from
employee
as
l
join
(
select
max
(
salary
,
5
)
as
salary
,
dept
from
employee
group
by
dept
)
as
r
on
l
.
dept
=
r
.
dept
and
l
.
salary
=
r
.
salary
join
(
select
max
(
salary
,
5
)
as
salary
,
dept
from
employee
group
by
dept
)
as
r
on
l
.
dept
=
r
.
dept
and
l
.
salary
=
r
.
salary
```
### 结构错误
```
sql
select
id
,
name
,
dept
,
salary
,
rank
()
over
(
partition
by
dept
order
by
salary
desc
)
as
r
select
id
,
name
,
dept
,
salary
,
rank
()
over
(
partition
by
dept
order
by
salary
desc
)
as
r
from
employee
where
r
<=
5
;
```
...
...
@@ -75,7 +88,13 @@ where r <= 5;
### 结构错误
```
sql
select
id
,
name
,
dept
,
salary
,
rank
()
as
r
over
(
partition
by
dept
order
by
salary
desc
)
select
id
,
name
,
dept
,
salary
,
rank
()
as
r
over
(
partition
by
dept
order
by
salary
desc
)
from
employee
where
r
<=
5
;
```
data/3.MySQL高阶/2.SQL高级技巧/3.透视表 /config.json
浏览文件 @
ba97f6ea
...
...
@@ -9,7 +9,9 @@
"export"
:
[
"pivot.json"
],
"keywords_must"
:
[
"mysql"
],
"keywords_must"
:
[
"mysql"
],
"keywords_forbid"
:
[],
"group"
:
1
}
\ No newline at end of file
data/3.MySQL高阶/2.SQL高级技巧/3.透视表 /pivot.md
浏览文件 @
ba97f6ea
...
...
@@ -25,18 +25,30 @@ create index on sales(created_at);
```
sql
select
sku_id
,
sum
(
case
extract
(
month
from
created_at
)
when
1
then
amount
else
0
end
)
as
Jan
,
sum
(
case
extract
(
month
from
created_at
)
when
2
then
amount
else
0
end
)
as
Feb
,
sum
(
case
extract
(
month
from
created_at
)
when
3
then
amount
else
0
end
)
as
Mar
,
sum
(
case
extract
(
month
from
created_at
)
when
4
then
amount
else
0
end
)
as
Apr
,
sum
(
case
extract
(
month
from
created_at
)
when
5
then
amount
else
0
end
)
as
May
,
sum
(
case
extract
(
month
from
created_at
)
when
6
then
amount
else
0
end
)
as
June
,
sum
(
case
extract
(
month
from
created_at
)
when
7
then
amount
else
0
end
)
as
July
,
sum
(
case
extract
(
month
from
created_at
)
when
8
then
amount
else
0
end
)
as
Aug
,
sum
(
case
extract
(
month
from
created_at
)
when
9
then
amount
else
0
end
)
as
Sept
,
sum
(
case
extract
(
month
from
created_at
)
when
10
then
amount
else
0
end
)
as
Oct
,
sum
(
case
extract
(
month
from
created_at
)
when
11
then
amount
else
0
end
)
as
Nov
,
sum
(
case
extract
(
month
from
created_at
)
when
12
then
amount
else
0
end
)
as
Dec
sum
(
case
extract
(
month
from
created_at
)
when
1
then
amount
else
0
end
)
as
Jan
,
sum
(
case
extract
(
month
from
created_at
)
when
2
then
amount
else
0
end
)
as
Feb
,
sum
(
case
extract
(
month
from
created_at
)
when
3
then
amount
else
0
end
)
as
Mar
,
sum
(
case
extract
(
month
from
created_at
)
when
4
then
amount
else
0
end
)
as
Apr
,
sum
(
case
extract
(
month
from
created_at
)
when
5
then
amount
else
0
end
)
as
May
,
sum
(
case
extract
(
month
from
created_at
)
when
6
then
amount
else
0
end
)
as
June
,
sum
(
case
extract
(
month
from
created_at
)
when
7
then
amount
else
0
end
)
as
July
,
sum
(
case
extract
(
month
from
created_at
)
when
8
then
amount
else
0
end
)
as
Aug
,
sum
(
case
extract
(
month
from
created_at
)
when
9
then
amount
else
0
end
)
as
Sept
,
sum
(
case
extract
(
month
from
created_at
)
when
10
then
amount
else
0
end
)
as
Oct
,
sum
(
case
extract
(
month
from
created_at
)
when
11
then
amount
else
0
end
)
as
Nov
,
sum
(
case
extract
(
month
from
created_at
)
when
12
then
amount
else
0
end
)
as
Dec
from
sales
where
created_at
between
'2020-01-01'
::
timestamp
and
'2021-01-01'
::
timestamp
group
by
sku_id
;
...
...
@@ -78,18 +90,30 @@ group by sku_id, extract(month from created_at);
```
sql
select
sku_id
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
1
)
as
Jan
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
2
)
as
Feb
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
3
)
as
Mar
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
4
)
as
Apr
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
5
)
as
May
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
6
)
as
June
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
7
)
as
July
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
8
)
as
Aug
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
9
)
as
Sept
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
10
)
as
Oct
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
11
)
as
Nov
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
12
)
as
Dec
sum
(
amount
having
extract
(
month
from
created_at
)
=
1
)
as
Jan
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
2
)
as
Feb
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
3
)
as
Mar
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
4
)
as
Apr
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
5
)
as
May
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
6
)
as
June
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
7
)
as
July
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
8
)
as
Aug
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
9
)
as
Sept
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
10
)
as
Oct
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
11
)
as
Nov
,
sum
(
amount
having
extract
(
month
from
created_at
)
=
12
)
as
Dec
from
sales
where
created_at
between
'2020-01-01'
::
timestamp
and
'2021-01-01'
::
timestamp
group
by
sku_id
,
extract
(
month
from
created_at
);
...
...
data/3.MySQL高阶/2.SQL高级技巧/4.Double Not Exists/DoubleNotExists.md
浏览文件 @
ba97f6ea
...
...
@@ -46,14 +46,14 @@ Joe 想要找出参与了所有投标的企业(有围标嫌疑),那么这
select
*
from
company
where
not
exists
(
select
*
from
invitation
where
not
exists
(
select
*
from
bids
where
invitation
.
id
=
bids
.
invitation_id
and
bids
.
company_id
=
company
.
id
)
select
*
from
invitation
where
not
exists
(
select
*
from
bids
where
invitation
.
id
=
bids
.
invitation_id
and
bids
.
company_id
=
company
.
id
)
)
```
...
...
@@ -82,7 +82,9 @@ where id = any (select company_id
```
sql
select
*
from
company
where
company
.
id
in
(
select
distinct
company_id
from
bids
)
where
company
.
id
in
(
select
distinct
company_id
from
bids
)
```
### D
...
...
@@ -91,8 +93,8 @@ where company.id in (select distinct company_id from bids)
select
*
from
company
where
exists
(
select
*
from
bids
where
bids
.
company_id
=
company
.
id
)
select
*
from
bids
where
bids
.
company_id
=
company
.
id
)
```
\ No newline at end of file
data/3.MySQL高阶/2.SQL高级技巧/4.Double Not Exists/config.json
浏览文件 @
ba97f6ea
...
...
@@ -9,7 +9,9 @@
"export"
:
[
"DoubleNotExists.json"
],
"keywords_must"
:
[
"mysql"
],
"keywords_must"
:
[
"mysql"
],
"keywords_forbid"
:
[],
"groups"
:
2
}
\ No newline at end of file
data/3.MySQL高阶/2.SQL高级技巧/5.写入和冲突/score.md
浏览文件 @
ba97f6ea
...
...
@@ -22,7 +22,8 @@ create table book_in(
## 答案
```
sql
insert
into
book_in
(
login
,
score
)
values
(
$
1
,
$
2
)
on
DUPLICATE
KEY
update
score
=
$
2
;
insert
into
book_in
(
login
,
score
)
values
(
$
1
,
$
2
)
on
DUPLICATE
KEY
update
score
=
$
2
;
```
## 选项
...
...
data/3.MySQL高阶/3.过程化编程/5. 游标/cursor.md
浏览文件 @
ba97f6ea
...
...
@@ -74,7 +74,9 @@ end;
create
procedure
make_trade
()
begin
DECLARE
done
INT
DEFAULT
FALSE
;
declare
cur_orders
cursor
for
select
id
,
price
,
item_id
,
amount
from
orders
order
by
id
limit
1000
;
declare
cur_orders
cursor
for
select
id
,
price
,
item_id
,
amount
from
orders
order
by
id
limit
1000
;
declare
order_id
,
item_id
,
amount
INT
;
declare
price
decimal
(
12
,
4
);
DECLARE
CONTINUE
HANDLER
FOR
NOT
FOUND
SET
done
=
TRUE
;
...
...
@@ -100,7 +102,11 @@ end;
```
sql
create
procedure
make_trade
()
begin
declare
cur_orders
cursor
for
select
id
,
price
,
item_id
,
amount
from
orders
order
by
id
limit
1000
;
declare
cur_orders
cursor
for
select
id
,
price
,
item_id
,
amount
from
orders
order
by
id
limit
1000
;
declare
order_id
,
item_id
,
amount
INT
;
declare
price
decimal
(
12
,
4
);
DECLARE
CONTINUE
HANDLER
FOR
NOT
FOUND
SET
done
=
TRUE
;
...
...
@@ -122,7 +128,11 @@ end;
create
procedure
make_trade
()
begin
DECLARE
done
INT
DEFAULT
FALSE
;
declare
cur_orders
cursor
for
select
id
,
price
,
item_id
,
amount
from
orders
order
by
id
limit
1000
;
declare
cur_orders
cursor
for
select
id
,
price
,
item_id
,
amount
from
orders
order
by
id
limit
1000
;
declare
order_id
,
item_id
,
amount
INT
;
declare
price
decimal
(
12
,
4
);
DECLARE
CONTINUE
HANDLER
FOR
NOT
FOUND
SET
done
=
TRUE
;
...
...
@@ -143,7 +153,11 @@ end;
create
procedure
make_trade
()
begin
DECLARE
done
INT
DEFAULT
FALSE
;
declare
cur_orders
cursor
for
select
id
,
price
,
item_id
,
amount
from
orders
order
by
id
limit
1000
;
declare
cur_orders
cursor
for
select
id
,
price
,
item_id
,
amount
from
orders
order
by
id
limit
1000
;
declare
order_id
,
item_id
,
amount
INT
;
declare
price
decimal
(
12
,
4
);
DECLARE
CONTINUE
HANDLER
FOR
NOT
FOUND
SET
done
=
TRUE
;
...
...
data/3.MySQL高阶/3.过程化编程/5. 游标/cursor_desc.md
浏览文件 @
ba97f6ea
...
...
@@ -19,24 +19,16 @@
### A
```
"DECLARE 游标名字 CURSOR FOR 查询语句;" 用来声明游标
```
### B
```
定义好游标之后,需要使用"OPEN 游标名;"来打开游标
```
### C
```
游标使用有需要关闭"CLOSE 游标名;"
```
### D
```
游标就像指针一样,可以定位操作查询的数据
```
\ No newline at end of file
data/3.MySQL高阶/4.设计优化/1.生成列/generated.md
浏览文件 @
ba97f6ea
...
...
@@ -30,7 +30,8 @@ column_name data_type [GENERATED ALWAYS] AS (expression)
## 答案
```
sql
alter
table
points
add
modulus
double
generated
always
as
(
sqrt
(
x
*
x
+
y
*
y
));
alter
table
points
add
modulus
double
generated
always
as
(
sqrt
(
x
*
x
+
y
*
y
));
```
## 选项
...
...
@@ -38,17 +39,20 @@ alter table points add modulus double generated always as (sqrt(x*x + y*y));
### A
```
sql
create
generated
modulus
on
table
points
as
(
sqrt
(
x
*
x
+
y
*
y
));
create
generated
modulus
on
table
points
as
(
sqrt
(
x
*
x
+
y
*
y
));
```
### B
```
sql
alter
table
points
add
modulus
generated
always
as
(
sqrt
(
x
*
x
+
y
*
y
));
alter
table
points
add
modulus
generated
always
as
(
sqrt
(
x
*
x
+
y
*
y
));
```
### C
```
sql
alter
table
points
add
modulus
float
generated
sqrt
(
x
*
x
+
y
*
y
);
alter
table
points
add
modulus
float
generated
sqrt
(
x
*
x
+
y
*
y
);
```
data/3.MySQL高阶/4.设计优化/4.增加中间表/daily_payment2.md
浏览文件 @
ba97f6ea
...
...
@@ -38,26 +38,35 @@ group by date(payment_date);
建立中间表并建立索引。
```
postgresql
create table daily_payment(day date primary key , amount decimal(12, 4));
create table daily_payment(
day date primary key ,
amount decimal(12, 4));
insert into daily_payment(day, amount)
select payment_date::date as day, sum(amount) as amount from payment group by day;
select payment_date::date as day,
sum(amount) as amount
from payment group by day;
```
使用
```
sql
select
day
,
amount
from
view_daily_payment
where
day
between
$
1
and
$
2
;
select
day
,
amount
from
view_daily_payment
where
day
between
$
1
and
$
2
;
```
进行查询。并且每天定时执行一次刷新命令
```
sql
insert
into
daily_payment
(
day
,
amount
)
select
date
(
payment_date
)
as
day
,
sum
(
amount
)
as
amount
select
date
(
payment_date
)
as
day
,
sum
(
amount
)
as
amount
from
payment
where
date
(
payment_date
)
between
DATE_SUB
(
CURDATE
(),
INTERVAL
2
DAY
)
and
DATE_SUB
(
CURDATE
(),
INTERVAL
1
DAY
)
where
date
(
payment_date
)
between
DATE_SUB
(
CURDATE
(),
INTERVAL
2
DAY
)
and
DATE_SUB
(
CURDATE
(),
INTERVAL
1
DAY
)
group
by
day
;
```
...
...
@@ -69,7 +78,8 @@ group by day;
在 payment_date 列上建立索引
```
sql
create
index
idx_payment_date
on
payment
(
payment_date
);
create
index
idx_payment_date
on
payment
(
payment_date
);
```
### 不会优化 sum
...
...
@@ -77,7 +87,8 @@ create index idx_payment_date on payment(payment_date);
建立计算列
```
sql
alter
table
payment
add
day
date
generated
always
as
(
payment_date
::
date
)
stored
alter
table
payment
add
day
date
generated
always
as
(
payment_date
::
date
)
stored
```
然后使用它改写查询
...
...
@@ -94,7 +105,8 @@ group by day;
建立表达式索引
```
sql
create
index
idx_payment_day
on
payment
((
date
(
payment_date
)));
create
index
idx_payment_day
on
payment
((
date
(
payment_date
)));
```
### 不做物化,对查询速度不会有显著改善
...
...
@@ -102,7 +114,8 @@ create index idx_payment_day on payment((date(payment_date)));
建立视图
```
sql
create
view
view_daily_payment
as
select
date
(
payment_date
)
as
day
,
amount
from
payment
;
create
view
view_daily_payment
as
select
date
(
payment_date
)
as
day
,
amount
from
payment
;
```
然后在视图 view_daily_payment 上执行
...
...
data/3.MySQL高阶/5.运维与架构/6.复制过滤器/filter.md
浏览文件 @
ba97f6ea
...
...
@@ -84,7 +84,8 @@ SET GLOBAL replicate-ignore-db=goods,auth;
可以在从库执行以下命令,设定忽略 goods 数据库和 auth 数据库
```
sql
CHANGE
REPLICATION
FILTER
REPLICATE_IGNORE_DB
=
(
goods
,
auth
);
CHANGE
REPLICATION
FILTER
REPLICATE_IGNORE_DB
=
(
goods
,
auth
);
```
### J
...
...
@@ -108,7 +109,8 @@ SET GLOBAL replicate-do-table=goods.goods,goods.orders;
可以在在从库的 MySQL 命令行设定只同步 goods 表和 orders 表
```
sql
CHANGE
REPLICATION
FILTER
REPLICATE_DO_TABLE
=
(
goods
.
goods
,
goods
.
orders
);
CHANGE
REPLICATION
FILTER
REPLICATE_DO_TABLE
=
(
goods
.
goods
,
goods
.
orders
);
```
### M
...
...
@@ -124,7 +126,8 @@ replicate-do-table=goods.goods,goods.orders
可以在在从库的 MySQL 命令行设定只同步名称前缀为
`t_`
的表
```
sql
CHANGE
REPLICATION
FILTER
REPLICATE_WILD_DO_TABLE
=
(
goods
.
t_
%
);
CHANGE
REPLICATION
FILTER
REPLICATE_WILD_DO_TABLE
=
(
goods
.
t_
%
);
```
### P
...
...
@@ -140,7 +143,8 @@ SET GLOBAL replicate-ignore-table=goods.orders,goods.trade;
可以在从库的 MySQL 命令行设定忽略 orders 和 trade 表
```
sql
CHANGE
REPLICATION
FILTER
REPLICATE_
IGNORE_TABLE
=
(
goods
.
orders
,
goods
.
trade
);
CHANGE
REPLICATION
FILTER
REPLICATE_
IGNORE_TABLE
=
(
goods
.
orders
,
goods
.
trade
);
```
...
...
@@ -157,7 +161,8 @@ replicate-ignore-table =goods.orders,goods.trade
可以在在从库的 MySQL 命令行设定忽略名称前缀为
`o_`
的表
```
sql
CHANGE
REPLICATION
FILTER
REPLICATE_WILD_DO_TABLE
=
(
goods
.
o_
%
);
CHANGE
REPLICATION
FILTER
REPLICATE_WILD_DO_TABLE
=
(
goods
.
o_
%
);
```
### S
...
...
data/3.MySQL高阶/6.查询优化/3. EXPLAIN/explain.md
浏览文件 @
ba97f6ea
...
...
@@ -3,13 +3,14 @@
Joe 从交易服务中发现了一些高频查询和慢查询,例如
```
sql
with
recursive
r
(
id
)
as
(
select
id
from
orders
where
id
=
$
1
union
select
d
.
id
from
orders
as
d
join
r
on
d
.
id
=
r
.
id
+
1
)
with
recursive
r
(
id
)
as
(
select
id
from
orders
where
id
=
$
1
union
select
d
.
id
from
orders
as
d
join
r
on
d
.
id
=
r
.
id
+
1
)
select
orders
.
id
,
content
from
orders
join
r
on
orders
.
id
=
r
.
id
;
...
...
@@ -29,13 +30,14 @@ from orders
在测试库使用
```
sql
explain
with
recursive
r
(
id
)
as
(
select
id
from
orders
where
id
=
$
1
union
select
d
.
id
from
orders
as
d
join
r
on
d
.
id
=
r
.
id
+
1
)
explain
with
recursive
r
(
id
)
as
(
select
id
from
orders
where
id
=
$
1
union
select
d
.
id
from
orders
as
d
join
r
on
d
.
id
=
r
.
id
+
1
)
select
orders
.
id
,
content
from
orders
join
r
on
orders
.
id
=
r
.
id
;
...
...
@@ -44,13 +46,14 @@ from orders
进行剖分,对于需要了解详细执行计划的使用
```
sql
explain
analyze
with
recursive
r
(
id
)
as
(
select
id
from
orders
where
id
=
100
union
select
d
.
id
from
orders
as
d
join
r
on
d
.
id
=
r
.
id
+
1
)
explain
analyze
with
recursive
r
(
id
)
as
(
select
id
from
orders
where
id
=
100
union
select
d
.
id
from
orders
as
d
join
r
on
d
.
id
=
r
.
id
+
1
)
select
orders
.
id
from
orders
join
r
on
orders
.
id
=
r
.
id
;
...
...
@@ -73,13 +76,14 @@ from orders
使用
```
sql
analyze
with
recursive
r
(
id
)
as
(
select
id
from
orders
where
id
=
100
union
select
d
.
id
from
orders
as
d
join
r
on
d
.
id
=
r
.
id
+
1
)
analyze
with
recursive
r
(
id
)
as
(
select
id
from
orders
where
id
=
100
union
select
d
.
id
from
orders
as
d
join
r
on
d
.
id
=
r
.
id
+
1
)
select
orders
.
id
from
orders
join
r
on
orders
.
id
=
r
.
id
;
...
...
data/tree.json
浏览文件 @
ba97f6ea
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录