[英]MySQL temporary variable does not work when executed from PHP script
When executing the following query in MySQL the rows are updated correctly. 在MySQL中执行以下查询时,行将正确更新。
UPDATE market SET price =
CASE
WHEN
@order_price:=(
@market_price:=(IF(
market_id = 0,
(SELECT value FROM fiat WHERE id = 6),
(SELECT value FROM fiat WHERE id = 5)
)
+
(
(order_percentage_dynamic/100) * @market_price
))
) < IFNULL(exchange_rate_max, 999999999) AND @market_price > IFNULL(exchange_rate_min, -999999999)
THEN @market_price
WHEN @order_price > exchange_rate_max AND exchange_rate_max IS NOT NULL THEN exchange_rate_max
WHEN @order_price < exchange_rate_min AND exchange_rate_min IS NOT NULL THEN exchange_rate_min
END
WHERE bdynamicorder = true;
Executing that same query in a PHP script updates all rows but with NULL value. 在PHP脚本中执行相同的查询将更新所有行,但其值为NULL。
if ($update_stmt = $mysqli->prepare($query)) {
$update_stmt->execute();
}
Any ideas? 有任何想法吗?
Thanks! 谢谢!
I'm pretty sure that the problem is the lack of initialization of the variables. 我很确定问题是缺少变量的初始化。 Consider this expression:
考虑以下表达式:
@market_price := (IF(market_id = 0,
(SELECT value FROM fiat WHERE id = 6),
(SELECT value FROM fiat WHERE id = 5)
) +
(order_percentage_dynamic/100) * @market_price
)
If @market_price
has not been set, then this will return NULL
-- when either argument to an arithmetic operator is NULL
, the result is NULL
. 如果
@market_price
尚未设置,那么这将返回NULL
-当任一参数,以算术运算符是NULL
,则结果为NULL
。
One solution uses additional logic such as coalesce()
: 一种解决方案使用其他逻辑,例如
coalesce()
:
@market_price := IF(market_id = 0,
(SELECT value FROM fiat WHERE id = 6),
(SELECT value FROM fiat WHERE id = 5)
) +
(order_percentage_dynamic/100) * coalesce(@market_price, 0);
Alternatively, you can initialize the values using a join
: 另外,您可以使用
join
初始化值:
UPDATE market CROSS JOIN
(select @market_price := 0, @order_price := 0) vars
SET price = . . .
My guess is that when you run this on the database, the variables have already been set to non-NULL values. 我的猜测是,当您在数据库上运行此命令时,变量已设置为非NULL值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.