我有一块SQL可以计算排行榜上高尔夫球手的排名。我是Spring的新手,我不认为我可以使用Spring JDBC执行此操作,因此我认为我需要将其转换为mySQL存储过程。
有谁可以给我一些关于我需要转换的指示?简单地将CREATE PROCEDURE和param列表放在此处不起作用。
SET @rank = 1, @prev_val = NULL, @prev_rank = NULL;
SELECT rank FROM
(
SELECT @rank := IF(@prev_val!=winnings,@prev_rank+1,@rank) AS rank
, @prev_val := winnings AS winnings
, @prev_rank := @rank AS prevRank
, t.golferID
FROM
(
select g.golferID, sum(winnings) as winnings
from results r
join resultDetails rd on r.resultID = rd.resultID
join golfers g on rd.golferID = g.golferID
where r.status = 'C' and r.compID = 1
group by golferID order by winnings desc
) AS t
) AS showRank WHERE golferID = 16
答案 0 :(得分:1)
这会让你开始:
drop procedure if exists get_golfer_rank;
delimiter #
create procedure get_golfer_rank
(
p_golferID int unsigned
)
proc_main:begin
set @rank = 0;
select
...
where golferID = p_golferID;
end proc_main #
delimiter ;
call get_golfer_rank(18);
答案 1 :(得分:1)
你的训练很好 - f00假设你只想通过给定的参数调出每个高尔夫球手的等级 - 因此构造......
create procedure get_golfer_rank
(
p_golferID int unsigned
)
和...
where golferID = p_golferID;
限制单个高尔夫球手。
答案 2 :(得分:0)
感谢领导。我把它解决了。粗略地说它是
drop procedure if exists getRank;
delimiter $$
create procedure getRank()
begin
set @rank = 1;
set @prev_val = NULL;
set @prev_rank = NULL;
select rank from
(
select @rank := if(@prev_val!=winnings,@prev_rank+1,@rank) as rank
, @prev_val := winnings as winnings
, @prev_rank := @rank as prevrank
, t.golferid
from
(
select g.golferid, sum(winnings) as winnings
from results r
join resultdetails rd on r.resultid = rd.resultid
join golfers g on rd.golferid = g.golferid
where r.status = 'c' and r.compid = 1
group by golferid order by winnings desc
) as t
) as showrank where golferid = 16;
end; $$
delimiter ;