[英]using a variable in MYSQL limits in subquery
我正在嘗試重新創建在mySQL的SQL Server中所做的一些代碼。 我想為表格中的每一行插入一行。 我正在使用循環來執行此操作,在SQL Server中,我使用SELECT TOP @foo
這是我的mySQl
begin
set @maxloop = (select max(id) from `LeagueInfo`);
set @loopno = 1;
while @loopno <= @maxloop DO
SET @mtop = (select `teams` * `homegames` from `LeagueInfo` where id = @loopno);
SET @div = (select `LeagueShortName` from `LeagueInfo` where id = @loopno);
SET @teams = (select teams from `LeagueInfo` where id = @loopno);
SET @homegames = (select homegames from `LeagueInfo` where id = @loopno);
SET @fthgsum = (select sum(`FTHG`)/@teams/@homegames from `footy` where `id` in(select`id`, `div` from `footy`
where `div` = @DIV
order by `matchdate` desc LIMIT @mtop));
SET @ftagsum = (select sum(`FTAG`)/@teams/@homegames from `footy` where `id` in(select`id`, `div` from `footy`
where `div` = @DIV
order by `matchdate` desc LIMIT @mtop));
insert into `looptable` (`di`, `homeav`, `awayav`) values (@div, @fthgsum,@fthgsum);
set @loopno = @loopno +1;
END while;
END;
我在限制@mtop上遇到錯誤。 我讀過我可以通過准備好的語句來解決這個問題,但是我不確定如何在子查詢中做到這一點。 是否有要做這個的事情?還是有另一個我可以根據另一個表中的值為該表中的每一行選擇前x行的數量。
謝謝保羅
在您的代碼行中
SET @mtop = (select 'teams' * 'homegames' from 'LeagueInfo' where id = @loopno)
mtop
不是數字,這是錯誤的,因為limit必須是數字。
也許您想使用“ count(*)”?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.