将预准备语句结果保存为十进制变量

时间:2015-10-05 12:34:36

标签: mysql

据我所知,通过阅读其他答案,我们无法在预备语句中使用局部变量。由于用户变量没有类型,我们如何将语句的结果存储为十进制?

以下代码会将结果转换为整数。

set @mypenetration = null;
set @sql = 'select max(penetration) into ? from (select penetration from fssector2 where penetration > 0 order by penetration asc LIMIT ? ) as MyResult ;';
prepare stm1 from @sql;
execute stm1 using @myPenetration, @a;
SELECT @mypenetration;

我怎么能得到小数?

1 个答案:

答案 0 :(得分:0)

您可以直接在查询中使用用户定义的变量:

set @mypenetration = null;
set @sql = 'select CONVERT(max(penetration), DECIMAL(10,2)) into @mypenetration from (select penetration from fssector2 where penetration > 0 order by penetration asc LIMIT ? ) as MyResult ;';
prepare stm1 from @sql;
execute stm1 using @a;
SELECT @mypenetration;

例如,您也可以使用CONVERT()将值转换为DECIMAL(10,2)。