我这里有一个查询很困惑,不知道怎么写好了。
    表的结构大体是这样的,
    表1:姓名
    表2:姓名,业务1,业务2
    表3:姓名, 业务3,业务4
    表4:姓名,业务5,业务6
    ...
    现在我想从这几个表里查询出这样的结果:
    只要业务中有不为空的项,就将所有的人对应的所有的项目显示出来,如
     姓名,业务1,业务2,业务3,业务4,业务5,业务6...
    若查询表中的所有的业务都是null,则显示为空,即不讲姓名列出来
     
这样的查询应该怎么做好呢,请诸位高手指教。

解决方案 »

  1.   

    将多个表的数据按ID合并起来
    a
    id , value
    1  , 11
    2  , 21
    3  , 31
    4  , 41
    5  , 51b
    id , value
    1  , 12
    2  , 22
    3  , 32
    4  , 42c  , value
    1  , 13
    2  , 23
    3  , 33
    5  , 53d
    a , value1 , value2 , value3
    1 , 11     , 12     , 13
    2 , 21     , 22     , 23
    3 , 31     , 32     , 33
    4 , 41     , 42     , 0
    5 , 51     , 0      , 53
    CREATE TABLE #a ([id] [char] (10),[value] [int])
    CREATE TABLE #b ([id] [char] (10),[value] [int])
    CREATE TABLE #c ([id] [char] (10),[value] [int])insert into #a(id,value) values('1',11)  
    insert into #a(id,value) values('2',21)  
    insert into #a(id,value) values('3',31)  
    insert into #a(id,value) values('4',41)  
    insert into #a(id,value) values('5',51)  insert into #b(id,value) values('1',12)  
    insert into #b(id,value) values('2',22)  
    insert into #b(id,value) values('3',32)  
    insert into #b(id,value) values('4',42)  insert into #c(id,value) values('1',13)  
    insert into #c(id,value) values('2',23)  
    insert into #c(id,value) values('3',33)  
    insert into #c(id,value) values('5',53)  select isnull(isnull(#a.id,#b.id),#c.id) id,#a.value value1,#b.value value2,#c.value value3 
    from #a full join #b on #a.id=#b.id
    full join #c on isnull(#a.id,#b.id)=#c.iddrop table #a
    drop table #b
    drop table #cid         value1      value2      value3      
    ---------- ----------- ----------- ----------- 
    1          11          12          13
    2          21          22          23
    3          31          32          33
    4          41          42          NULL
    5          51          NULL        53(所影响的行数为 5 行)2个表,
    表A
       a1          a2
       1            9
       2           10
       3           67
       4           33
    表B
       b1          b2
        2           31
        4           99
    想实现这样
       a1         a2        b2
        1         9          NULL(或0或不体现任何数)
        2         10         31
        3         67         NULL(或0或不体现任何数)
        4         33         99
    谢谢各位指导~
    CREATE TABLE #a ([a1] [char] (10),[a2] [int])
    CREATE TABLE #b ([b1] [char] (10),[b2] [int])insert into #a(a1,a2) values('1',9)  
    insert into #a(a1,a2) values('2',10)  
    insert into #a(a1,a2) values('3',67)  
    insert into #a(a1,a2) values('4',33)  insert into #b(b1,b2) values('2',31)  
    insert into #b(b1,b2) values('4',99)  --显示NULL
    --select isnull(#a.a1,#b.b1) a1,#a.a2 a2,#b.b2 b2
    --from #a full join #b on #a.a1=#b.b1--显示0
    select #A.a1, #A.a2, isnull(#B.b2, 0)
    from #A left join #B on #A.a1 = #B.b1drop table #a
    drop table #b
      

  2.   

    请问dawugui(潇洒老乌龟) 
    表的前面加#号有什么作用啊?
    #a 这个#起什么作用
      

  3.   

    select a.name,a.y1,a.y2,a.y3,a.y4,a.y5,a.y6 from (select 表1.姓名 as name,表2.业务1 as y1,表2.业务2 as y2,表3.业务3 as y3,表3.业务4 as y4,表4.业务5 as y5,表4.业务6  as y6 from 
    表1 left outer join 表2 on 表1.姓名=表2.姓名 left outer join
     表3 on 表1.姓名=表3.姓名 left outer join
     表4 on 表1.姓名=表4.姓名) a  where a.y1 is not null and a.y2 is not null and a.y3 is not null and a.y4 is not null and  a.y5 is not null and a.y6 is not null
      

  4.   

    select a.name,a.y1,a.y2,a.y3,a.y4,a.y5,a.y6 from (select 表1.姓名 as name,表2.业务1 as y1,表2.业务2 as y2,表3.业务3 as y3,表3.业务4 as y4,表4.业务5 as y5,表4.业务6  as y6 from 
    表1 left outer join 表2 on 表1.姓名=表2.姓名 left outer join
     表3 on 表1.姓名=表3.姓名 left outer join
     表4 on 表1.姓名=表4.姓名) a  where a.y1 is not null or a.y2 is not null or a.y3 is not null or a.y4 is not null or a.y5 is not null or a.y6 is not null
      

  5.   

    select a.name,a.y1,a.y2,a.y3,a.y4,a.y5,a.y6 from (select 表1.姓名 as name,表2.业务1 as y1,表2.业务2 as y2,表3.业务3 as y3,表3.业务4 as y4,表4.业务5 as y5,表4.业务6  as y6 from 
    表1 left outer join 表2 on 表1.姓名=表2.姓名 left outer join
     表3 on 表1.姓名=表3.姓名 left outer join
     表4 on 表1.姓名=表4.姓名) a  where a.y1 is not null or a.y2 is not null or a.y3 is not null or a.y4 is not null or a.y5 is not null or a.y6 is not null