gpt4 book ai didi

MySQL - 如何消除具有 NULL 值的结果? (带代码)

转载 作者:行者123 更新时间:2023-11-29 01:18:43 31 4
gpt4 key购买 nike

这是我的SQL代码

SELECT p.plant_name, 
sum(data.value_1) totalvalue_1
FROM (SELECT DISTINCT date_format(date, '%Y-%m') ym
FROM data
WHERE data.id_fk = 1
AND date BETWEEN '2011-01-01' AND '2011-04-01') dates
CROSS JOIN (SELECT DISTINCT data.id_fk,
data.plant_id_fk
FROM data
WHERE data.id_fk = 1) up
JOIN plants p ON p.plant_id = up.plant_id_fk
LEFT JOIN data ON date_format(data.date, '%Y-%m') = dates.ym
AND up.id_fk = data.id_fk
AND up.plant_id_fk = data.plant_id_fk
AND category_1 = 'expenses'
GROUP BY up.plant_id_fk
ORDER BY up.id_fk, up.plant_id_fk

输出这个结果

aasd    74
qweqwe 20
tyutyu NULL
bnmbnm NULL
234234 NULL

我想完全消除包含 NULL 的结果,只得到这个作为我的结果:

aasd      74
qweqwe 20

我试过在几个子句中使用 is not null 但没有一个给出我需要的结果。

有什么建议吗?

最佳答案

尝试在 GROUP BYORDER BY 之间添加一个 HAVING 子句:

 group by up.plant_id_fk
HAVING SUM(data.value_1) > 0

关于MySQL - 如何消除具有 NULL 值的结果? (带代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5521258/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com