提交 a217dd69 编写于 作者: M Mars Liu

loop

上级 7ec23737
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
"node_id": "mysql-d7f86bc2fcbb4b83a11f2ed73bde7ac0", "node_id": "mysql-d7f86bc2fcbb4b83a11f2ed73bde7ac0",
"keywords": [], "keywords": [],
"children": [], "children": [],
"export": [], "export": [
"if.json"
],
"keywords_must": [], "keywords_must": [],
"keywords_forbid": [], "keywords_forbid": [],
"group": 0 "group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "if.md",
"notebook_enable": false,
"exercise_id": "19cc93e63f0946069ce30e4be5af4e2a"
}
\ No newline at end of file
# 判断
Goods 数据库中有一个名为 trade 的存储过程,封装了交易过程,每一笔交易,trade都会被调用一次。Joe
想在 trade 里加一段逻辑,实现:
1. 每一次交易,对 @counter 变量加一
2. 如果 @counter 是 1000 的整倍数,就将 @total_price 变量乘 0.8。
下面哪一段代码可以实现这个逻辑?
## 答案
```mysql
set @counter = @counter + 1;
if @counter % 1000 = 0 then
set @total_price = @total_price * 0.8;
end if;
```
## 选项
### A
```mysql
set @counter = @counter + 1;
if @counter % 1000 = 0 {
set @total_price = @total_price * 0.8;
}
```
### B
```mysql
set @counter = @counter + 1;
if (@counter % 1000 = 0) {
set @total_price = @total_price * 0.8;
}
```
### C
```mysql
if @counter % 1000 = 0 begin ;
set @total_price = @total_price * 0.8;
end;
```
### D
```mysql
set @counter ++;
if @counter % 1000 = 0 then begin
select @total_price = @total_price * 0.8;
end;
```
\ No newline at end of file
{ {
"node_id": "mysql-8ef7f9a1bba04bd782d80e9459446228", "node_id": "mysql-8ef7f9a1bba04bd782d80e9459446228",
"keywords": ["loop", "循环"], "keywords": [
"loop",
"循环"
],
"children": [], "children": [],
"export": [], "export": [
"loop.json"
],
"keywords_must": [], "keywords_must": [],
"keywords_forbid": [], "keywords_forbid": [],
"group": 0 "group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "loop.md",
"notebook_enable": false,
"exercise_id": "7cca3eff58b1408ea98a8fa363b2c771"
}
\ No newline at end of file
# Loop 循环
封装订单交易过程的 trade procedure 中有一个循环,会遍历订单中的商品项目,如果当前的商品价格 `@price`
大于 1000,则将其价格乘 0.9 ,如果 `@total_price` 变量中的总价已经超过 20000,则跳出循环。否则进行
下一个商品项目的处理。这个循环应该是(这里不考虑每个项目本身的处理逻辑,仅考虑折扣和商品订单分割):
## 答案
```mysql
-- ...
trade: LOOP
if @price > 1000 then
set @price = @price * 0.9;
end if;
-- ...
if @total_price > 20000 then
LEAVE trade;
else
ITERATE trade;
end if;
-- ...
end LOOP trace;
```
## 选项
### A
```mysql
-- ...
trade: LOOP
if @price > 1000 then
set @price = @price * 0.9;
end if;
-- ...
if @total_price > 20000 then
LEAVE trade;
end if;
-- ...
end LOOP trace;
```
### B
```mysql
-- ...
trade: LOOP
if @price > 1000 then
set @price = @price * 0.9;
end if;
-- ...
if @total_price > 20000 then
ITERATE trade;
else
LEAVE trade;
end if;
-- ...
end LOOP trace;
```
### C
```mysql
-- ...
trade: LOOP
if @price > 1000 then
set @price = @price * 0.9;
end if;
-- ...
if @total_price > 20000 then
LEAVE trade;
else
NEXT trade;
end if;
-- ...
end LOOP trace;
```
### D
```mysql
-- ...
trade: LOOP
if @price > 1000 then
set @price = @price * 0.9;
end if;
-- ...
if @total_price > 20000 then
LEAVE trade;
end if;
NEXT trade;
-- ...
end LOOP trace;
```
{
"node_id": "mysql-2f35dac71e6e42b189f7615fcf27a4e6",
"keywords": ["leave", "break"],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
{
"node_id": "mysql-cf916e42a060467ab25b16112780f246",
"keywords": [],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
{ {
"node_id": "mysql-8bbe6f55f32b4a4a8a6011c5748b36fa", "node_id": "mysql-8bbe6f55f32b4a4a8a6011c5748b36fa",
"keywords": [], "keywords": ["while"],
"children": [], "children": [],
"export": [], "export": [],
"keywords_must": [], "keywords_must": [],
......
...@@ -2177,29 +2177,6 @@ ...@@ -2177,29 +2177,6 @@
"group": 0 "group": 0
} }
}, },
{
"LEAVE": {
"node_id": "mysql-2f35dac71e6e42b189f7615fcf27a4e6",
"keywords": [
"leave",
"break"
],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
},
{
"ITERATE": {
"node_id": "mysql-cf916e42a060467ab25b16112780f246",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
},
{ {
"REPEAT": { "REPEAT": {
"node_id": "mysql-923c7bbf96cf4630a5dfb36f15e62d24", "node_id": "mysql-923c7bbf96cf4630a5dfb36f15e62d24",
...@@ -2508,7 +2485,11 @@ ...@@ -2508,7 +2485,11 @@
{ {
"GROUP BY优化": { "GROUP BY优化": {
"node_id": "mysql-66fc4566eaf34994b072ca83bf79ceb4", "node_id": "mysql-66fc4566eaf34994b072ca83bf79ceb4",
"keywords": [], "keywords": [
"performance",
"优化",
"group by"
],
"children": [], "children": [],
"keywords_must": [], "keywords_must": [],
"keywords_forbid": [], "keywords_forbid": [],
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册