PostgreSQL的MySQL ISNULL SQL代码等效性



我是PostgreSQL的新手,我一直在学习。我遵循一个使用MySQL的小型项目指南,但我更喜欢在PostgreSQL中工作。我读过很多StackOverflow的答案,这些答案在过去对我有帮助,但我完全被这一个卡住了。我的代码是:

Select a.parcelid, a.propertyaddress, b.parcelid, b.propertyaddress,
ISNULL(a.propertyaddress, b.propertyaddress)
From public.nashhousing a
JOIN public.nashhousing b 
on a.parcelid = b.parcelid
AND a.uniqueid <> b.uniqueid
Where a.propertyaddress is null

我一直收到的错误消息是:

ERROR:  function isnull(text, text) does not exist
LINE 2: ISNULL(a.propertyaddress, b.propertyaddress)
^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

有人能帮我吗?非常感谢。

COALESCE是该函数的sql标准版本,可能适用于大多数数据库。即coalesce(a.propertyaddress, b.propertyaddress)

COALESCE返回第一个非null值,因此它可以使用两个(在您的情况下(或多个值。例如,我们也可以写coalesce(a.propertyaddress, b.propertyaddress, 'no address!'),如果前两个值都为null,它将返回第三个值。

以下是官方的postgres文档:

https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-聚结-NVL-IFNULL

我认为您的示例是错误的,因为您正在寻找IFNULL而不是ISNULL的替代方案,因为ISNULL只接受一个参数。备用的SQL标准函数是COALESCE。

最新更新