正在编写 mysql 程序
Writing mysql procedure
我 table:
name position rating try_count
Bob 87 15 20
Erick 115 46 28
John 95 32 17
Karl 208 25 5
我们需要编写一个存储过程来计算最有效率的球员的名字。使用公式 (position / try_count) * rating 计算效率。获得的数字越高,玩家的效率就越高。先决条件:必须使用循环(while) 和临时table 执行计算。我不知道该怎么做,Google 也帮不了我:(
谢谢大家!
这是一些符合您规定要求的代码。
DROP TABLE IF EXISTS T;
DROP PROCEDURE IF EXISTS P;
CREATE TABLE T
(name VARCHAR(10),position INT,rating INT,try_count INT);
INSERT INTO T VALUES
('Bob' , 87 , 15 , 20),
('Erick' ,115 , 46 , 28),
('John' , 95 , 32 , 17),
('Karl' ,208 , 25 , 5);
delimiter $$
create procedure p()
begin
declare i int;
set i = 1;
drop temporary table if exists temp;
create temporary table temp as
select *,
(position / try_count) * rating as performance
from t;
while I = 1 do
select * from temp order by performance desc limit 1;
set i = 2;
end while;
end $$
delimiter ;
call p();
+------+----------+--------+-----------+-------------+
| name | position | rating | try_count | performance |
+------+----------+--------+-----------+-------------+
| Karl | 208 | 25 | 5 | 1040.0000 |
+------+----------+--------+-----------+-------------+
1 row in set (0.048 sec)
当然是废话..
我 table:
name position rating try_count
Bob 87 15 20
Erick 115 46 28
John 95 32 17
Karl 208 25 5
我们需要编写一个存储过程来计算最有效率的球员的名字。使用公式 (position / try_count) * rating 计算效率。获得的数字越高,玩家的效率就越高。先决条件:必须使用循环(while) 和临时table 执行计算。我不知道该怎么做,Google 也帮不了我:(
谢谢大家!
这是一些符合您规定要求的代码。
DROP TABLE IF EXISTS T;
DROP PROCEDURE IF EXISTS P;
CREATE TABLE T
(name VARCHAR(10),position INT,rating INT,try_count INT);
INSERT INTO T VALUES
('Bob' , 87 , 15 , 20),
('Erick' ,115 , 46 , 28),
('John' , 95 , 32 , 17),
('Karl' ,208 , 25 , 5);
delimiter $$
create procedure p()
begin
declare i int;
set i = 1;
drop temporary table if exists temp;
create temporary table temp as
select *,
(position / try_count) * rating as performance
from t;
while I = 1 do
select * from temp order by performance desc limit 1;
set i = 2;
end while;
end $$
delimiter ;
call p();
+------+----------+--------+-----------+-------------+
| name | position | rating | try_count | performance |
+------+----------+--------+-----------+-------------+
| Karl | 208 | 25 | 5 | 1040.0000 |
+------+----------+--------+-----------+-------------+
1 row in set (0.048 sec)
当然是废话..