【发布时间】:2019-09-03 22:03:27
【问题描述】:
mysql 函数有错误。我在下面附上了我的代码。
DELIMITER $$
CREATE FUNCTION CheckAccount6(ids INT)
RETURNS integer
BEGIN
DECLARE opening INT;
DECLARE a INT;
SET a = 0;
select q.invoice, q.timestamp,
(select sum(o.totprice) from sp_orderdetails o where o.quotation_no = q.id) as amount
from sp_quotation q where q.cid = ids and q.invoice != '0'
UNION
select 0, t.timestamp, t.amount from sp_transactions t where t.cid = ids and t.status like 'Approved';
IF(invoice = 0) THEN
a = a - amount;
ELSE
a = a + amount;
return a;
END $$
【问题讨论】:
-
您不能在函数中使用
SELECT语句,除非您使用INTO将结果存储在变量中。 -
这个函数应该做什么?如果您只关心
invoice和amount,为什么还要选择所有其他字段?如果查询返回多于一行,您希望这些变量包含什么? -
选中那些变量后想写一些循环条件
-
您需要使用游标来循环查询结果。
-
但是为什么不在查询中使用
SUM()而不是循环呢?