我有一个包含项目数据的表,如果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
您可以在下面尝试-
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')
要么
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')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句