【发布时间】:2020-09-16 19:18:17
【问题描述】:
所以我有一个看起来像这样的程序
CREATE PROCEDURE slct()
BEGIN
DECLARE ttl INT DEFAULT 0;
SELECT NULL AS price, NULL AS account_id, NULL as max_amount, NULL as total
FROM sellers
UNION ALL
SELECT price, account_id, max_amount, ttl := ttl + max_amount
FROM (SELECT * FROM sellers ORDER BY price ASC) C where selling_currency_id = :buying and buying_currency_id = :buying_with and ttl < :quantity
END
由于这里的海象运算符,它给了我一个语法错误:ttl := ttl + max_amount。如果这是一个用户变量 (@ttl),那么该 walrus 运算符可以工作,但 @ttl 没有作用域这一事实会在我的查询中引入一些错误。
所以我想知道:我如何使 := 工作/用等效的东西替换它?
编辑:
我发现了 INTO 关键字,现在我的查询看起来像这样。仍然说我有语法错误...
CREATE PROCEDURE select_buy(
IN buying BIGINT UNSIGNED,
IN buying_with BIGINT UNSIGNED,
IN quantity BIGINT UNSIGNED,
INOUT ttl BIGINT UNSIGNED
)
BEGIN
SELECT NULL AS price, NULL AS account_id, NULL as max_amount, NULL as total
FROM sellers
UNION ALL
SELECT price, account_id, max_amount, (SELECT (ttl + max_amount) INTO ttl) AS total
FROM (SELECT * FROM sellers ORDER BY price ASC) C where selling_currency_id = buying and buying_currency_id = buying_with and ttl < quantity
END
【问题讨论】:
-
只有用户定义的变量可以用作赋值运算符的目标,而不是局部变量。 @ttl 没有作用域的事实会在我的查询中引入一些错误。 ??这怎么可能?请提供一个建模小提琴。
-
:buying、:buying_with和:quantity是什么?这些看起来像 PDO 占位符,但我认为您不能在过程中使用它们。 -
我想是因为并发?我认为@total 没有任何锁定?还是我完全错了? (是的,那些是占位符)
-
它们是会话变量,每个连接都有自己的变量。
-
您不必担心您的
@total会干扰别人的。
标签: mysql sql sum subquery window-functions