0

我有一个查询,当没有匹配的项目 ID 时,我似乎无法让它显示“无项目”:

(select (case when prj.Proj_ID is null or prj.Proj_ID =  '' 
    then 'No Project' 
    ELSE prj.Proj_ID + ', ' + prj.[DESC] 
    END) 
from prj 
where prj.Proj_ID = other.Proj_ID)  as 'Project Description'

任何想法?

4

3 回答 3

0

试试这样:

SELECT
  CASE WHEN (other.Proj_ID IS NULL OR other.Proj_ID = '') THEN 'No Project'
    ELSE other.Proj_ID + ', ' + prj.[DESC]
  END as 'Project Description'
FROM
  other
  LEFT JOIN prj on ISNULL(prj.Proj_ID,0) = ISNULL(other.Proj_ID,0)
于 2012-08-01T10:56:26.783 回答
0
SELECT
  CASE WHEN (prj.Proj_ID IS NULL OR prj.Proj_ID = '') THEN 'No Project'
    ELSE prj.Proj_ID + ', ' + prj.[DESC]
  END as 'Project Description'
FROM
  other
  LEFT JOIN prj on prj.Proj_ID = other.Proj_ID

如果您要从 OTHER 数据库加入 PRJ,则需要离开加入它。如果在 PRJ 表中找不到 Proj_ID,case 语句将返回“No Project”。括号将有助于解决 的条件语句prj.Proj_ID

在您的原始查询中,您似乎没有指定要加入哪个表,而是通过 statement 指示加入where prj.Proj_ID = other.Proj_ID) as 'Project Description'

于 2012-07-31T18:40:04.577 回答
0

您还应该检查项目描述是否为空,否则在与它连接时可能会返回空prj.Proj_ID + ', '

SELECT CASE
         WHEN prj.Proj_ID IS NULL
               OR prj.Proj_ID = '' THEN 'No Project'
         ELSE prj.Proj_ID + COALESCE(', ' + prj.[DESC], '')
       END AS 'Project Description'
FROM   prj
       LEFT OUTER JOIN other
         ON prj.Proj_ID = other.Proj_ID 

(我也这样做了,所以如果描述为空,它也不会显示逗号)

于 2012-08-01T11:23:07.750 回答