有两个表:
表:  Tab1:                        Tab2:
字段:File1, Code1                 Code2,  Name2
      sz19  001                     001      财政
      sz19  002                     002      工商
      sz20  003                     003      物价
      sz20  001                    。
      。。
这样的查询结果
     sz19     001;002     财政; 工商
     sz20     001;003     财政; 物价
    ........
SQL怎么写呢?(VC6+Access2000)

解决方案 »

  1.   

    select a.file1,a.code1,b.code2,b.name2 from tab1 a,tab2 b
    where a.code1=b.code2 ordery by a.file1,b.code2;
    你试一试,看行不行
    其实这个可以用BRID报表工具完成!很简单的,写SQL反而不好写了!
      

  2.   

    yzh_yinke2002(yzh):
         我试试去,“BRID”是什么东西,做什么用的?
      

  3.   

    这样的结果是
    sz19     001  001     财政
    sz19     002  002     工商
    。。不行呀!
      

  4.   

    你可以分步做啊:
    1。创建一个临时表temp_table(表结构为你想显示的结构)
    2。将table1中的字段放入,在将table2中的字段放入(按你的要求)
    3。显示
      

  5.   

    sammon(努力学习)、hansonhx(天涯) :
        可不可以给个Exmaple?
      

  6.   

    CREATE FUNCTION sumstr(@name nvarchar(50))  
    RETURNS nvarchar(3000)
    AS  
    BEGIN
    declare @sumstr nvarchar(3000)
    declare @sumstr1 nvarchar(3000)
    set @sumstr=''
    set @sumstr1=''
    select @sumstr=@sumstr+case when @sumstr<>'' then ';' else '' end+code1 from tab1 where file1=@name
    select @sumstr1=@sumstr1+case when @sumstr1<>'' then ';' else '' end+name2 from tab2
         where code2 in (select code1 from tab1 where file1=@name)
    set @sumstr=@sumstr+'   '+@sumstr1
    return(@sumstr)
    END
    GO
    select distinct file1, dbo.sumstr(file1)
    from tab1 group by file1 
    GO
      

  7.   

    CREATE FUNCTION sumstr(@name nvarchar(50))  
    RETURNS nvarchar(3000)
    AS  
    BEGIN
    declare @sumstr nvarchar(3000)
    declare @sumstr1 nvarchar(3000)
    set @sumstr=''
    set @sumstr1=''
    select @sumstr=@sumstr+case when @sumstr<>'' then ';' else '' end+code1 from tab1 where file1=@name
    select @sumstr1=@sumstr1+case when @sumstr1<>'' then ';' else '' end+name2 from tab2
         where code2 in (select code1 from tab1 where file1=@name)
    set @sumstr=@sumstr+'   '+@sumstr1
    return(@sumstr)
    END
    GO
    select distinct file1, dbo.sumstr(file1)
    from tab1 group by file1 
    GO
      

  8.   

    CREATE FUNCTION sumstr(@name nvarchar(50))  
    RETURNS nvarchar(3000)
    AS  
    BEGIN
    declare @sumstr nvarchar(3000)
    declare @sumstr1 nvarchar(3000)
    set @sumstr=''
    set @sumstr1=''
    select @sumstr=@sumstr+case when @sumstr<>'' then ';' else '' end+code1 from tab1 where file1=@name
    select @sumstr1=@sumstr1+case when @sumstr1<>'' then ';' else '' end+name2 from tab2
         where code2 in (select code1 from tab1 where file1=@name)
    set @sumstr=@sumstr+'   '+@sumstr1
    return(@sumstr)
    END
    GO
    select distinct file1, dbo.sumstr(file1)
    from tab1 group by file1 
    GO
      

  9.   

    横向显示真的很困难的,
    建临时表的办法不太可行,如果sz19还有004、005、006,
    那岂不是临时表的字段数是个动态的变化量?而且这个建临时表的sql也
    不太好写,感觉上我没办法,呵呵BRIO我没怎么用过,但我用过BO,不知道两者有没有什么大差别,
    在BO里这东西也不太好弄,因为要把字段的不同值作为新字段来处理。
    直接用SQL表示我觉得很难,望高手指教。
      

  10.   

    OpenVMS(半知半解) :可惜,VB没用过,不过还要谢谢!
    fang_jb(寂寞如雪):直接用SQL表示是可以的,以前我在这里看过一个贴子,跟我的问题几乎就一样,可现在怎么也找不到了!
      

  11.   

    select tabl.File1,tabl.Code1,tabltemp.Code1 as Code1temp,
           Tab2.Name2,Tab2temp.Name2
    from Tab1 left join tabl as tab1temp on Tab1.File1 = tab1temp.File1
              left join Tab2 on Tab1.Code1 =Tab2.Code2
              left join tab2 as tab2temp on 
                             Tab1temp.Code1 =Tab2temp.Code2
      

  12.   

    OpenVMS的办法是Oracle的写法,不是VB,他的办法是可行的,
    不过在Access 里可能不好用,标准SQL应该用存储过程来写了
      

  13.   

    用两个临时表(比较苯的方法)第一个放入yzh_yinke2002(yzh) 所给的查询结果,如
    sz19     001  001     财政
    sz19     002  002     工商然后对其循环分析,存储过程也可,第二个临时表3个子段即可
      

  14.   

    yelintao(石川):我去试试吧!
    fang_jb(寂寞如雪) :不好意思,我还以为是VB里用的呢,看来还的要去修炼修炼!
    Rodgu(古鹏) :这么方法也想过,如果记录量很大的时候,会不会很慢?
      

  15.   

    Rodgu(古鹏) :这么方法也想过,如果记录量很大的时候,会不会很慢?一条条的去分析,肯定快不了,所以说是比较苯的方法,不过竖表转横表,好像也没什么太好的方法