为什么即使有结果,count(*)仍返回0?

时间:2019-03-05 14:19:49

标签: mysql

最近我曾尝试在mySQL中创建一个存储过程,但是我不了解很多事情,现在我想知道为什么当我执行此过程时,即使有大于0的结果也等于0一百结果? 这是过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `test4`(out resultat int)
begin
select value, timestamp 
from sensorParser 
where sensor='SOILTC' 
and id_wasp='SPARK_SA'  ;
select resultat=count(*);
end

如果您知道,您能清楚地向我解释一下吗?

我是这样改变的:

CREATE DEFINER=`root`@`localhost` PROCEDURE `test4`(out resultat int)
begin
select value, timestamp, resultat=count(* ) from sensorParser 
where sensor='SOILTC'
and id_wasp='SPARK_SA'  ;
end

但是它仍然不起作用,它只显示一个结果,即:

value = 19.437
timestamp = 2019-02-04 07:40:06
resultat=count(*) = NULL

1 个答案:

答案 0 :(得分:1)

您的存储过程中有2个查询。

第一个看起来不错(我们没有表定义,也没有数据样本来验证它。

第二个查询select resultat=count(*);不好,没有from子句。计数(*)从零开始就是0!

注意;是结束查询定界符。

尝试此操作以获取所有值和计数

CREATE DEFINER=`root`@`localhost` PROCEDURE `test4`(out resultat int)
begin
select value, timestamp from sensorParser 
where sensor='SOILTC'
and id_wasp='SPARK_SA'  ;

select count(*) into resultat from sensorParser 
where sensor='SOILTC'
and id_wasp='SPARK_SA'  ;

end

而且,在调用存储过程时:

call test4(@resultat);
select @resultat;
相关问题