尝试打印从 1 到 1000 的素数,并用“&”连接起来。错误是“错误代码:1292。截断了不正确的 DOUBLE 值:'&'”

问题描述 投票:0回答:1
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 primes
1个回答
0
投票

至少有两个问题。

首先,也是导致你错误的直接原因,在mysql中

+
是一个数字运算。要进行字符串连接,请执行以下操作:

set p = concat(p, '&', x);

其次,您需要通过

while x
循环每次迭代将 n 设置为 1。按原样,第一个 x 值达到 1001,并且对于后面的值,有效地跳过
while n<=1000
循环。

© www.soinside.com 2019 - 2024. All rights reserved.