SQL如何使null或empty变为整数零

时间:2016-07-11 09:59:43

标签: mysql sql

这是我的查询

  SELECT dp.comment,
     dp.tjah,
     dp.tjih,
     dp.tjor
     FROM dp
     LEFT OUTER JOIN rd ON (dp.uid = rd.uid)
     LEFT OUTER JOIN ri ON (rd.uid = ri.uid)
     LEFT OUTER JOIN rh ON (ri.uid = rh.uid)
     LEFT OUTER JOIN dv ON (rh.name = dv.name)
     WHERE rd.report_datetime='2014-06-20'
     LIMIT 5

这是输出

comment             tjsb                       tjih                       tjor 
------------------  --------------------------  --------------------  ---------------------
HWDP 31 FT                                  16                (NULL)                 (NULL)
15 ft pup joint                         (NULL)                (NULL)                     22
drill pipe range 3                          24                     0                    593
drill collar 31 ft                      (NULL)                    15                 (NULL)
pony DC 10 ft                           (NULL)                (NULL)                      2

正如您所看到的,有很多空值如何修改查询以使null变为0或0.00并且它是整数或双精度值而不是字符串。

2 个答案:

答案 0 :(得分:4)

您可以使用coalesce

SELECT dp.comment
     , coalesce(dp.total_joints_at_shore_base, 0)
     , coalesce(dp.total_joints_in_hole, 0)
     , coalesce(dp.total_joints_on_rig, 0)
<...>

答案 1 :(得分:2)

在MYSQL中使用IFNULL()

SELECT dp.comment,
     ifnull(dp.total_joints_at_shore_base,0),
     ifnull(dp.total_joints_in_hole,0),
     ifnull(dp.total_joints_on_rig,0)
     FROM drill_pipe_daily_summary dp
     LEFT OUTER JOIN report_daily rd ON (dp.daily_uid = rd.daily_uid)
     LEFT OUTER JOIN rig_information ri ON (rd.rig_information_uid = ri.rig_information_uid)
     LEFT OUTER JOIN region_history rh ON (ri.rig_information_uid = rh.rig_information_uid)
     LEFT OUTER JOIN division dv ON (rh.region_name = dv.division_name)
     WHERE rd.report_datetime='2014-06-20'