日期:2014-05-18  浏览次数:20897 次

inner join和case能一起使用吗吗?
Select   P.*,T.Name   From   PartsCTE   as   P
inner   join  
  CASE   TypeId
  WHEN   1   THEN   [Regional]
                  WHEN   2   THEN   [Tours]
  end   as   T
on   T.ID=P.ObjectId

Regional,Tours为表名

------解决方案--------------------
Select P.*,T.Name From PartsCTE as P
inner join Regional t on T.ID=P.ObjectId
union
Select P.*,T.Name From PartsCTE as P
inner join Tours t on T.ID=P.ObjectId

------解决方案--------------------
不能这样用,修改成如下方式:

Select
P.*,
case TypeId
when 1 then (select Name from [Regional] where ID=P.ObjectId)
when 2 then (select Name from [Tours] where ID=P.ObjectId)
end as Name
From
PartsCTE as P

select P.*,T.Name from PartsCTE P,[Regional] T where T.ID=P.ObjectId
union all
select P.*,T.Name from PartsCTE P,[Tours] T where T.ID=P.ObjectId

------解决方案--------------------
Select P.*,T.Name From PartsCTE P
inner join [Regional] T on T.ID=P.ObjectId WHERE P.TypeId=1 UNION ALL
Select P.*,T.Name From PartsCTE P
inner join [Regional] T on T.ID=P.ObjectId WHERE P.TypeId=2
------解决方案--------------------
Select P.*,T.Name From PartsCTE P
inner join [Regional] T on T.ID=P.ObjectId WHERE P.TypeId=1 UNION ALL
Select P.*,T.Name From PartsCTE P
inner join [Tours] T on T.ID=P.ObjectId WHERE P.TypeId=2
------解决方案--------------------
Select P.*,T.Name From PartsCTE as P
inner join
(
select 1 as typeid,* from [Regional]
union all
selct 2 as typeid from [Tours]
) T
on T.ID=P.ObjectId and T.typeid =P.typeid


*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

最新版本:20070130

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
------解决方案--------------------
INNER JOIN两侧应该是结果集
而CASE返回的是单一记录

------解决方案--------------------
可以的,以下是非常正确的答案
Select
P.*,
case TypeId
when 1 then (select Name from [Regional] where ID=P.ObjectId)
when 2 then (select Name from [Tours] where ID=P.ObjectId)
end as Name
From
PartsCTE as P