select a.id,b.服务名称 as 项目名称,a.单位 from a left join b on a.项目ID = b.服务ID where a.单位 = '钟'
union 
select a.id,c.物品名称 as 项目名称,a.单位 from a left join c on a.项目ID = c.物品ID where a.单位 <> '钟'

解决方案 »

  1.   

    恩!这个方法我也想过!不知道除了使用union以外有没有其它的方法呢?我想知道!我现在测试一下你这个方法!
      

  2.   

    case when a.单位 = '钟' then b.服务名称 else c.物品名称 end as 项目名称
    ----
    问题后来要和不同的表关联 我就想不到什么好办法
      

  3.   

    --查询
    select 项目ID=a.id,项目名称=case a.单位 when '钟' then b.服务名称 else c.物品名称 end,项目单位=a.单位
    from 表A a
    left join 表B b on a.单位='钟' and a.项目ID=b.服务ID
    left join 表C c on a.单位<>'钟' and a.项目ID=c.物品ID
      

  4.   

    --示例--示例数据
    create table 表A(ID int,单位 char(2),项目ID int)
    insert 表A select 1,'钟',1
    union  all select 2,'钟',2
    union  all select 3,'钟',3
    union  all select 4,'瓶',1
    union  all select 5,'件',2
    union  all select 6,'碗',3create table 表B(服务ID int,服务名称 varchar(10))
    insert 表B select 1,'按摩'
    union  all select 2,'洗浴'
    union  all select 3,'捶骨'create table 表C(物品ID int,物品名称 varchar(10))
    insert 表C select 1,'洗发水'
    union  all select 2,'衣服'
    union  all select 3,'水鱼汤'
    go--查询
    select 项目ID=a.id,项目名称=case a.单位 when '钟' then b.服务名称 else c.物品名称 end,项目单位=a.单位
    from 表A a
    left join 表B b on a.单位='钟' and a.项目ID=b.服务ID
    left join 表C c on a.单位<>'钟' and a.项目ID=c.物品ID
    go--删除测试
    drop table 表A,表B,表C/*--测试结果项目ID        项目名称       项目单位 
    ----------- ---------- ---- 
    1           按摩         钟
    2           洗浴         钟
    3           捶骨         钟
    4           洗发水        瓶
    5           衣服         件
    6           水鱼汤        碗(所影响的行数为 6 行)
    --*/
      

  5.   

    楼上的很直观:
    select 项目ID=a.id,
    项目名称=case a.单位 when '钟' then b.服务名称 
           else c.物品名称 
         end,
    项目单位=a.单位
    from 表A a
    left join 表B b on a.单位='钟' and a.项目ID=b.服务ID
    left join 表C c on a.单位<>'钟' and a.项目ID=c.物品ID
      

  6.   

    视图不允许使用case 的!我晕哦!不过刚才使用了一下视图设计器,感觉使用得挺爽啊!哈哈!
    以前有些不会的,只要你懂得你要做什么东西,使用那个就可以实现好多东西啦!
    谢谢各位了!
      

  7.   

    回复人: kfarvid(永远爱着一个叫"傲"的女孩) ( ) 信誉:100  2004-08-19 15:55:00  得分: 0  
    视图不允许使用case 的!我晕哦!
    --
    不会啊 可以的!