如何将查询结果存储到mysql中的变量中

时间:2016-08-12 14:04:38

标签: mysql sql stored-procedures stored-functions

我是mysql的新手,我在phpmyadmin中编写以下函数:

BEGIN
declare @var1 int;
declare @var2 int;

set @var1 = 0;
set @var2 = 0;

count(*) 
into @var1
from transactions 
where reg_date > DATE_SUB(CURDATE(),INTERVAL 90 DAY) 
and reg_date <= CURDATE() = last_transactions;

count(*) 
into @var2
from account 
where exp_date > CURDATE() 
AND reg_date > DATE_SUB(CURDATE(),INTERVAL 90 DAY);


return @var2/@var1;
END

我总是收到1064错误。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

BEGIN
declare @var1 int;
declare @var2 int;

set @var1 = 0;
set @var2 = 0;

select count(*) as count into @var1  
from transactions 
where reg_date > DATE_SUB(CURDATE(),INTERVAL 90 DAY) 
and reg_date <= CURDATE() = last_transactions;

select count(*) as count into @var2
from account 
where exp_date > CURDATE() 
AND reg_date > DATE_SUB(CURDATE(),INTERVAL 90 DAY);
return @var2/@var1;
END