mysql - 用 0 更改 Null

标签 mysql

我已尝试按如下方式在 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/

相关文章:

php - 使用 php 在数据库中自动增加表的下一个

php - 由当前登录的用户安全更新或插入行 'owned'

java - 准备好的语句似乎在 java servlet 中总是返回 null

mysql - 求和日期范围而不计算mysql中的重叠

php - 如何从我的 mysql 数据库中回显变量

mysql - 根据Mysql中的两列获取最小值

php - 仅向用户显示数据库中的记录一次

mysql - 如何使用纯sql查询显示启用的应用程序?

java - 我需要从选择查询中返回所有值的方法

MySQL - 每日前 10 名