sql 查询双精度 - null >0 不返回任何记录

sql query double precision - null >0 not returning any record

我正在使用 Postgresql 数据库并且有一个列 invoicehead。我的做法如下:

SELECT * FROM invoicehead WHERE (invamt-collection_amt >= 0)

invamt type = double precision
collection_amt type = double precision

invamt 的值为 100.00、500.00、300.00、-250.00 和 collection_amt 的值全部为 null

因此,当我执行 invamt-collection_amt >= 0 时,它将 双精度值 - null

我没有得到任何值作为查询结果。

如何查询才能让if null取0的效果?

您可以添加第二个条件:

SELECT * 
FROM invoicehead 
WHERE invamt-collection_amt >= 0 
   OR (invamt-collection_amt) IS NULL

或者您可以使用 COALESCE:

NULL 更改为 0
SELECT * 
FROM invoicehead 
WHERE COALESCE(invamt,0) - COALESCE(collection_amt,0) >= 0 

NULL 值无法使用 =,<>,<,>, <=,>=,...

等比较运算符直接与其他值进行比较

使用COALESCEIF条件

SELECT * 
FROM invoicehead 
WHERE (COALESCE(invamt,0) - COALESCE(collection_amt,0)) >= 0 

或者

SELECT * 
FROM invoicehead 
WHERE (IF(invamt IS NOT NULL, invamt ,0) - IF(collection_amt IS NOT NULL, collection_amt,0)) >= 0