mysql添加空值查询

wkftcu5l  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(251)

我有mysql查询

SELECT
    ju.ID,
    ju.JENIS_USAHA,
    COALESCE(TOTAL_PEMBERI, 0) AS TOTAL_PEMBERI,
    COALESCE(OUTLET_PEMBERI, 0) AS OUTLET_PEMBERI,
    COALESCE(TOTAL_PENERIMA, 0) AS TOTAL_PENERIMA,
    COALESCE(OUTLET_PENERIMA, 0) AS OUTLET_PENERIMA,
    COALESCE(OUTLET_PEMBERI + OUTLET_PENERIMA, 0) AS SEMUA

问题是当outlet_pemberi not null和outlet_penerima null(反之亦然)时,semua将显示0。如何解决这个问题?谢谢您

vltsax25

vltsax251#

尝试使用 COALESCE 分别在两个条款上:

SELECT
    ju.ID,
    ju.JENIS_USAHA,
    COALESCE(TOTAL_PEMBERI, 0) AS TOTAL_PEMBERI,
    COALESCE(OUTLET_PEMBERI, 0) AS OUTLET_PEMBERI,
    COALESCE(TOTAL_PENERIMA, 0) AS TOTAL_PENERIMA,
    COALESCE(OUTLET_PENERIMA, 0) AS OUTLET_PENERIMA,
    COALESCE(OUTLET_PEMBERI, 0) + COALESCE(OUTLET_PENERIMA, 0) AS SEMUA
FROM ...

如果这不能解决你的问题,你真的想要一个零值,当求和中的任何一项 NULL ,然后保持代码不变。

相关问题