我有一个包含项目数据的表,如果找不到project_ID
,我想返回NULL或“ Not_Exist”。
例如:
SELECT a.[Project ID], a.[Stage]
FROM Projects a
WHERE a.[Project ID] IN ('CR324S', 'a')
CR324S
存在于表中,但'a'
不存在,所以我想要的结果将是:
Project ID Stage
-----------------------------
CR324S Implementation
a Not_Exist
答案 0 :(得分:2)
您可以在下面尝试-
SELECT a.[pid], coalesce(b.[Stage],'Not_Exist') as stage
from
(
select 'CR324S' as pid
union
select 'a'
)A left join Projects B on A.pid=B.[Project ID] and [Project ID] IN ('CR324S','a')
OR
SELECT a.[pid], coalesce(b.[Stage],'Not_Exist') AS stage
FROM (VALUES ('CR324S'), ('a')) A(pid)
LEFT JOIN Projects B on A.pid=B.[Project ID] AND [Project ID] IN ('CR324S','a')