Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_mysql
提交
e75cabce
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看板
提交
e75cabce
编写于
5月 31, 2022
作者:
M
Mars Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cursor
上级
5bd28275
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
181 addition
and
4 deletion
+181
-4
data/3.MySQL高阶/4. 过程化编程/5. 游标/config.json
data/3.MySQL高阶/4. 过程化编程/5. 游标/config.json
+7
-2
data/3.MySQL高阶/4. 过程化编程/5. 游标/cursor.json
data/3.MySQL高阶/4. 过程化编程/5. 游标/cursor.json
+8
-0
data/3.MySQL高阶/4. 过程化编程/5. 游标/cursor.md
data/3.MySQL高阶/4. 过程化编程/5. 游标/cursor.md
+160
-0
data/tree.json
data/tree.json
+6
-2
未找到文件。
data/3.MySQL高阶/4. 过程化编程/5. 游标/config.json
浏览文件 @
e75cabce
{
"node_id"
:
"mysql-459fd14ff096438d9b6460270bec4754"
,
"keywords"
:
[
"cursor"
],
"keywords"
:
[
"cursor"
,
"游标"
],
"children"
:
[],
"export"
:
[],
"export"
:
[
"cursor.json"
],
"keywords_must"
:
[],
"keywords_forbid"
:
[],
"group"
:
0
...
...
data/3.MySQL高阶/4. 过程化编程/5. 游标/cursor.json
0 → 100644
浏览文件 @
e75cabce
{
"type"
:
"code_options"
,
"author"
:
"ccat"
,
"source"
:
"cursor.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"106eadb83e2c4928b503d89dda2dd8f3"
}
\ No newline at end of file
data/3.MySQL高阶/4. 过程化编程/5. 游标/cursor.md
0 → 100644
浏览文件 @
e75cabce
# 游标
Joe 需要开发一个存储过程 make_trade,从 orders 表
```
mysql
create table orders (
id int primary key auto_increment,
price decimal(12, 4),
item_id int,
amount int
)
```
中,按 id 从小到大加载订单数据,生成交易单,写入 trade 表
```
mysql
create table trade (
id int primary key auto_increment,
total decimal(12, 4)
-- ...
)
```
其中 trade 的 total字段保存了交易单的总金额,这个金额是每一张交易单的所有 orders 的 price 总和,其数值不会超过 20000。
因为撮合交易的逻辑是串行的,Joe决定用游标实现这部分逻辑。下列哪一个选项是对的(选项中仅包含撮合逻辑,不包括完整的存储过程)?
为简单起见,我们忽略交易的详细逻辑,假设 orders 表仅包含 id, price, item_id, amount 四项。
## 答案
```
mysql
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 order_id, item_id, amount INT;
declare price decimal(12, 4);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cur_orders;
trade: LOOP
fetch cur_orders into order_id, price, item_id, amount;
if done then
LEAVE trade;
end if;
if @total + @price > 2000 then
leave trade;
else
-- 省略交易逻辑
end if;
END LOOP trade;
CLOSE cur_orders;
start transaction;
-- save trade
delete from orders where id < order_id;
commit ;
end;
```
## 选项
### A
```
mysql
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 order_id, item_id, amount INT;
declare price decimal(12, 4);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cur_orders;
trade: LOOP
fetch cur_orders into order_id, price, item_id, amount;
if done then
LEAVE trade;
end if;
-- 省略交易逻辑
if @total + @price > 2000 then
leave trade;
else
NEXT cur_orders;
end if;
END LOOP trade;
end;
```
### B
```
mysql
create procedure make_trade()
begin
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;
trade: LOOP
fetch cur_orders into order_id, price, item_id, amount;
-- 省略交易逻辑
END LOOP trade;
start transaction;
-- save trade
delete from orders where id < order_id;
commit ;
end;
```
### C
```
mysql
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 order_id, item_id, amount INT;
declare price decimal(12, 4);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cur_orders;
fetch cur_orders into order_id, price, item_id, amount;
if done then
LEAVE cur_orders;
end if;
-- 省略交易逻辑
end;
```
### D
```
mysql
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 order_id, item_id, amount INT;
declare price decimal(12, 4);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cur_orders;
trade: LOOP
fetch cur_orders into order_id, price, item_id, amount;
if done then
LEAVE trade;
end if;
-- 省略交易逻辑
END LOOP trade;
CLOSE cur_orders;
start transaction;
-- save trade
delete from orders where id < order_id;
commit ;
end;
```
\ No newline at end of file
data/tree.json
浏览文件 @
e75cabce
...
...
@@ -2180,7 +2180,10 @@
{
"REPEAT"
:
{
"node_id"
:
"mysql-923c7bbf96cf4630a5dfb36f15e62d24"
,
"keywords"
:
[],
"keywords"
:
[
"repeat"
,
"循环"
],
"children"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[],
...
...
@@ -2203,7 +2206,8 @@
" 游标"
:
{
"node_id"
:
"mysql-459fd14ff096438d9b6460270bec4754"
,
"keywords"
:
[
"cursor"
"cursor"
,
"游标"
],
"children"
:
[],
"keywords_must"
:
[],
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录