查询中的 MySQL 循环变量

Ner*_*ron 6 mysql sql loops

我想在我的 select 语句的结果中使用循环索引“i”,以便将它插入到另一个表中。是这样的:

set i=0;
while i<25 do

    insert into a (x,y,z)
    select a,b,i
    from table1;

    set i= i+1;
end while;
Run Code Online (Sandbox Code Playgroud)

有什么方法可以做到?

Ner*_*ron 2

完毕 :)

我刚刚创建了变量 i 作为 @i ,一切都解决了。

像这样:

set @i=0;
while @i<25 do

    insert into a (x,y,z)
    select a,b,@i
    from table1;

    set @i= @i+1;
end while;
Run Code Online (Sandbox Code Playgroud)

无论如何,谢谢:)