Sql 如何将ISNULL()函数与join一起使用
在使用ISNULL函数和我给定的列名别名形成左连接时,出现了一个错误 错误是: 我无法理解我做错了什么。数据存储在customers表中: 我使用的sql代码是:Sql 如何将ISNULL()函数与join一起使用,sql,Sql,在使用ISNULL函数和我给定的列名别名形成左连接时,出现了一个错误 错误是: 我无法理解我做错了什么。数据存储在customers表中: 我使用的sql代码是: select ISNULL(c2.name,'N/A') as referredby , c1.name as name from customers as c1 left outer join customers as c2 on c1.referredby = c2.id order by referredby; 不
select ISNULL(c2.name,'N/A') as referredby , c1.name as name
from customers as c1
left outer join customers as c2 on c1.referredby = c2.id
order by referredby;
不要使用isnull()
!标准版本是coalesce()
:
我做了另外两个改变。首先,表别名更好地描述了表的角色。我还将列别名更改为
referedby\u name
,因此它与表中的列不匹配。这两项都不是必需的;有了它们,我认为查询更容易阅读。您使用的是哪种数据库管理系统?(ISNULL是一个特定于产品的函数。)尝试COALESCE(c2.name,'N/a')
而不是ISNULL
似乎您在使用DB2,而ISNULL并不存在。使用coalesce()。coalesce有效。非常感谢。
select coalesce(cref.name, 'N/A') as referredby_name, c.name as name
from customers c left outer join
customers cref
on c.referredby = cref.id
order by referredby_name;