Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何将ISNULL()函数与join一起使用_Sql - Fatal编程技术网

Sql 如何将ISNULL()函数与join一起使用

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; 不

在使用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;
不要使用
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;