0

我正在尝试在 mySQL 上重新创建我在 SQL Server 中所做的一些代码。我想为表中的每一行插入一行。我正在使用循环来执行此操作,在 SQL 服务器中我使用了 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 行。

谢谢保罗

4

1 回答 1

0

在您的代码行中,

SET @mtop = (select 'teams' * 'homegames' from 'LeagueInfo' where id = @loopno)

mtop不是数字,这是错误的,因为 limit 需要是数字。

也许你想使用“count(*)”?

于 2013-08-08T19:45:37.203 回答