我已尝试按如下方式在 phpmyAdmin 中运行查询
SELECT
orders_history.id,
orders_history.`items`,
orders_history.`transaction_id`,
orders_history.`quantity`,
estockClaims.`esquantity`,
IFNULL( esquantity, 0 ),
orders_history.`quantity` - estockClaims.`esquantity` AS myquantity
FROM orders_history
LEFT JOIN estockClaims
ON orders_history.transaction_id = estockClaims.transaction_id
AND orders_history.items = estockClaims.items
LIMIT 0 , 100
它给了我这个结果:
----------------------------------------------------------------------
id items transaction_id quantity esquantity IFNULL(esquantity , 0 ) myquantity
1 FR 001 100 NULL 0 NULL
2 BR 002 10 NULL 0 NULL
3 WR 003 25 25 25 0
4 CR 004 50 3 3 47
如何解决这个问题,使 NULL 不是 NULL anyomre,而是更改为 0。在此先感谢。
谢谢
最佳答案
您已经在下一栏中找到了它。您需要做的是删除原始的 esquantity
列并为 IFNULL...
列创建别名,如下所示:
SELECT orders_history.id, orders_history.`items` , orders_history.`transaction_id` ,
orders_history.`quantity` , IFNULL( esquantity, 0 ) AS esquantity,
orders_history.`quantity` - estockClaims.`esquantity` AS myquantity
FROM orders_history
LEFT JOIN estockClaims ON orders_history.transaction_id = estockClaims.transaction_id
AND orders_history.items = estockClaims.items
LIMIT 0 , 100
我提到的更改在上面的第 2 行中。
更新
得到
orders_history.`quantity` - estockClaims.`esquantity` AS myquantity
要显示预期结果,您需要再次“取消取消”esquantity
字段,这样减法才能起作用:
orders_history.`quantity` - IFNULL( estockClaims.`esquantity`, 0 ) AS myquantity
这将确保您不再获得,例如:
100 - NULL
而是这个:
100 - 0
这将返回一个正确的值。
如果 esquantity
为 NULL,您也可以跳过整个减法操作,只需使用 quantity
的值即可。
关于mysql - 用 0 更改 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26972829/