use world;
delimiter $$
create procedure a()
begin
declare x int default 1;
declare n int default 1;
declare y int default 0;
declare p varchar(2000) default '';
case
when x<=1000 then
while x<=1000 do
while n<=1000 do
if x%n=0 then set y=y+1;
else set y=y;
end if;
set n = n+1;
end while;
if y=2 then set p = p + '&' + cast(x as char);
else set y=2;
end if;
set x=x+1;
end while;
else set y=2;
end case;
select p ;
end $$
call a();
我尽了一切努力来找到错误。我发现很多在线资源都存在新的解决方案,但我想找出我的代码中的错误,以便我可以从错误中学习。
至少有两个问题。
首先,也是导致你错误的直接原因,在mysql中
+
是一个数字运算。要进行字符串连接,请执行以下操作:
set p = concat(p, '&', x);
其次,您需要通过
while x
循环每次迭代将 n 设置为 1。按原样,第一个 x 值达到 1001,并且对于后面的值,有效地跳过 while n<=1000
循环。