我有一个名为Percentages的表,其中一个名为5StarResults的列,我想用存储过程名称5star的结果填充它。
当我使用'Call 5star'时,它只返回一个百分比,我希望这个百分比插入百分比表。
请帮忙,我试过编辑程序以包含一个Insert但它总是返回说插入了0行。
谢谢
编辑,
存储过程如下
BEGIN
declare Rating5Total int default 5;
declare Rating5Win int default 5;
declare 5StarPerformance decimal;
set Rating5Total = (select COUNT(Ratings) from vHighPerformance where Ratings = 7);
set Rating5Win = (select COUNT(Results) from vHighPerformance where Ratings = 7 and Results = 1);
set 5StarPerformance = Rating5Win*100.0/Rating5Total;
Select 5StarPerformance;
END
答案 0 :(得分:0)
如果选项是在同一存储过程中执行INSERT,则以下代码应该起作用:
CREATE PROCEDURE `5star`()
BEGIN
DECLARE Rating5Total INT DEFAULT 5;
DECLARE Rating5Win INT DEFAULT 5;
DECLARE 5StarPerformance DECIMAL(18, 3);
SET Rating5Total := (SELECT COUNT(Ratings) FROM vHighPerformance WHERE Ratings = 7);
SET Rating5Win := (SELECT COUNT(Results) FROM vHighPerformance WHERE Ratings = 7 AND Results = 1);
SET 5StarPerformance := Rating5Win*100.0/Rating5Total;
/* INSERT INTO percentages (percentage) VALUES (5StarPerformance); */
INSERT INTO percentages (id, percentage) VALUES (1, 5StarPerformance)
ON DUPLICATE KEY UPDATE percentage = 5StarPerformance;
SELECT 5StarPerformance;
END$$
在SQL Fiddle中可以看到一个例子。
<强>更新强>
如果您需要在表percentages
上创建一条记录,则可以进行UPSERT。请参阅更新的代码。