在使用ISNULL函数以及为形成左联接而给出的列名别名时,会出现错误。
我正在使用的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;
答案 0 :(得分:1)
请勿使用isnull()
!标准版本为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;
我做了另外两个更改。首先,表别名更好地描述了表的角色。我还将列别名更改为referredby_name
,因此它与表中的列不匹配。这些都不是必需的。与他们在一起,我认为查询更容易阅读。