Mysql 将Null更改为0

Mysql 将Null更改为0,mysql,Mysql,我已尝试运行一个查询I phpmyAdmin,如下所示 它给了我这个结果: ---------------------------------------------------------------------- id items transaction_id quantity esquantity IFNULL(esquantity , 0 ) myquantity 1 FR

我已尝试运行一个查询I phpmyAdmin,如下所示

它给了我这个结果:

       ----------------------------------------------------------------------
       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的值。

您已经在下一列中找到了它。您需要做的是删除原始的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的值。

您可以使用if检查esquantity和myquantity列:

或者使用德国DanFromGermany所说的IFNULL

您可以使用IF检查esquantity和myquantity列:


或者像Danfrom Germany所说的那样使用IFNULL,原因是您将其用作select,而不是在执行减法时使用。按如下方式使用:

SELECT orders_history.id, orders_history.`items` , orders_history.`transaction_id` ,          orders_history.`quantity` , orders_history.`quantity` - IFNULL( esquantity, 0 ) 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

原因是您将其用作选择,而不是在执行减法时使用。按如下方式使用:

SELECT orders_history.id, orders_history.`items` , orders_history.`transaction_id` ,          orders_history.`quantity` , orders_history.`quantity` - IFNULL( esquantity, 0 ) 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
还可以使用COALESCE将空值替换为0

检查此查询

SELECT orders_history.id, orders_history.`items` , orders_history.`transaction_id` , 
       orders_history.`quantity` , COALESCE( esquantity, 0 ) AS esquantity, 
       orders_history.`quantity` - COALESCE(estockClaims.`esquantity`, 0) 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
还可以使用COALESCE将空值替换为0

检查此查询

SELECT orders_history.id, orders_history.`items` , orders_history.`transaction_id` , 
       orders_history.`quantity` , COALESCE( esquantity, 0 ) AS esquantity, 
       orders_history.`quantity` - COALESCE(estockClaims.`esquantity`, 0) 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

如果是空的。。。与IFNULL…,…;+答案没有帮助,在哪里使用它?我想你实际上应该让upvoter解释为什么这个答案得到了upvote-@DanFromGermany向你解释了为什么它完全没有帮助,或者我应该说完全没有用,因为OP已经在做IFNULL。。。你应该更专注于给出好的答案,而不是追逐分数。顺便说一句,我想说你应该很高兴你得到了这项投票,不要要求更多。如果。。。是空的。。。与IFNULL…,…;+答案没有帮助,在哪里使用它?我想你实际上应该让upvoter解释为什么这个答案得到了upvote-@DanFromGermany向你解释了为什么它完全没有帮助,或者我应该说完全没有用,因为OP已经在做IFNULL。。。你应该更专注于给出好的答案,而不是追逐分数。顺便说一句,我想说你应该很高兴你得到了这张选票,不要要求更多。它适用于esquantity专栏。但是为什么'orders\u history.quantity`-estockClaims.esquantity AS myquantity``的结果仍然为空。我想要的是第一个数量仍然相同。因为您还需要在那里使用0而不是NULL,我将为您编写一个更新。它适用于esquantity列。但是为什么'orders\u history.quantity`-estockClaims.esquantity AS myquantity``的结果仍然为空。我想要的是第一个数量仍然相同。因为你也需要在那里使用0而不是NULL,我会给你写一个更新。没有否决,但你应该总是添加一个解释,只有代码的答案很可能会被否决。没有否决,但你应该总是添加一个解释,只有代码的答案很可能被否决。