我有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不为null而OUTLET_PENERIMA为null(反之亦然)时,SEMUA将显示0。如何解决此问题?谢谢
答案 0 :(得分:1)
尝试分别在两个词上使用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
时,您确实想要一个零值,则只需保留代码即可。