带有多个INNER JOINS的select语句中带有case的SQL Server查询

时间:2015-06-18 11:43:27

标签: sql-server tsql select case inner-join

我有一个查询,返回一组有关项目的数据。但问题是某些项目没有有效的货币ID。

所以我正在尝试构建一个查询,如果货币ID为0,则使用id 140。这是我到目前为止,但如果货币ID为0,则不返回任何结果。

   SELECT *,
   CASE
   WHEN p.currencyid=0 THEN 140
   END
   FROM projects AS p 
   INNER JOIN businesssectors AS bs ON bs.businesssectorid=p.businesssectorid 
   INNER JOIN currencies AS c ON c.currencyid=p.currencyid
   INNER JOIN plants AS pl ON p.plantid=pl.plantid 
   WHERE p.projectid='195'

1 个答案:

答案 0 :(得分:2)

试试这个,它应该可行

   SELECT *,
   CASE
   WHEN p.currencyid=0 THEN 140 ELSE p.currencyid 
   END
   FROM projects AS p 
   INNER JOIN businesssectors AS bs ON bs.businesssectorid=p.businesssectorid 
   INNER JOIN plants AS pl ON p.plantid=pl.plantid 
   LEFT OUTER JOIN currencies AS c ON c.currencyid=p.currencyid
   WHERE p.projectid='195'