A表  id    f1
     1    100B表  id    f2
     1    200
     1    200就是说 B表是A 的从表,A 中ID是唯一的,B 中ID 可多条
现在比如说A表ID号为1 f1值为100,B表有五条
我用连接取A和B 表字段,结果是五行,每行都会看到A表的数据
如果这时我统计A表的F1字段就会重复五次,B表显示正常.
有没办法得到结果是以下这样的?
ID  f1    f2
1   100   200
          200 
          ...就是说A表字段只出来一次???谢谢大家..

解决方案 »

  1.   


    select a.id,a.f1,b.f2 from a,b where a.id=b.id
      

  2.   


    select   a.id,a.f1,b.f2   from   a,b   where   a.id=b.id 你写的这句就是会有重复的啊,如何通过SQL取消重复的呢?
    要通过报表啊?不知道有没人有用K3,他在表格上显示,就能将重复行去掉,不知道它是如何做到的!还有人知道不?
      

  3.   

    use test
    go create   Table #t(ID int,f1 int)
    insert #t select 1,100create table #t2 (ID int,f2 int)
    insert #t2 select 1,200
    union all select 1,200select *,row=1 into #2 from #t2 order by ID
    go
    declare @i int,@j int
    update #2 set @i=case when ID=@j then @i+1 else 1 end,row=@i,@j=ID
    select 
    [ID]=case when row=(select min(row)from #2 where ID=t2.ID) then rtrim(t.ID) else ''end,
    [f1]=case when row=(select min(row)from #2 where ID=t2.ID) then rtrim(t.f1) else '' end,
    [f2]=t2.f2
    from 
    #t t
    join 
    #2 t2 on t.ID=t2.ID
    ID           f1           f2          
    ------------ ------------ ----------- 
    1            100          200
                              200(所影响的行数为 2 行)
      

  4.   

    还有人知道不?roy_88 能否将两表实际操作的写写啊?
    你这样是固定列固定记录的写,不行呀。:)