GroupA 表:
  GAID
  GAName
  GAOther
  ... 
  
GroupB 表:
  GBID
  GBName
  GBOther
  ... Item 表
  ItemID
  ItemType
  ItemOther
  ...如果ItemTyp = 1时,从GroupA 表取得相应的数据(GroupA.*, Item.*)on Item.ItemID = GroupA.GAID when Item.ItemType = 1如果ItemTyp = 2时,从GroupB 表取得相应的数据(GroupB.*, Item.*)on Item.ItemID = GroupB.GBID when Item.ItemType = 2这个查询语句应该怎么写,case...when..不能选择表名字吗?=======
Select Item.*, gg.*(???) from Item
left join 
  case Item.ItemType
    when 1 then GroupA as gg on Item.ItemID = GroupA.GAID (报错!!!)
    when 2 then GroupB as gg on Item.ItemID = GroupB.GAID (报错!!!)
  end
=========谢谢!

解决方案 »

  1.   

    select *
    from (
    select 1 as ItemType ,GAID,GAName,GAOther from GroupA 
    union all
    select 2 as ItemType ,GBID,GBName,GBOther from GroupB
    ) t inner join Item on t.ItemType=Item.ItemType
      

  2.   

    顶一下,能不能用case...when...语句?
      

  3.   

    不能
    select * from item left join on GroupA Item.ItemID = GroupA.GAID where Item.ItemType=1select * from item left join on Groupb Item.ItemID = Groupb.GAID where Item.ItemType=2如果两表结构一致
    select *
    from (select 1 as ItemType ,GAID,GAName,GAOther from GroupA 
        union all
        select 2 as ItemType ,GBID,GBName,GBOther from GroupB) a inner join Item b on a.ItemType=b.ItemType
      

  4.   


    select GAID,GAName,GAOther from GroupA inner join Item on Item.ItemID = GroupA.GAID where Item.ItemType = 1
    union all
    select GBID,GBName,GBOther from GroupB inner join Item on Item.ItemID = GroupB.GAID where Item.ItemType = 2