我有2个表,是通过dh(单号)连接在一起的,如下:表1(t1):
id   name   age   dh
1    小王    22   101
2    小李    23   101
3    小赵    24   102
4    小黑    26   102
...
表2(t2):
dh    id   class
101   1    1年1班
102   2    2年1班最后我想显示的结果如下:t1.id   t1.name   t1.age    t2.id    t2.class
1        小王       22        1      1年1班
2        小李       23        ..        ..     
3        小赵       24        2      2年1班
4        小黑       26        ..        ..也就是经过连接后,把t2表里第2行或第3行是重复第1行的数据 就不显示出来了!请指教!

解决方案 »

  1.   


    ;with AcHerat as
    (
        select *,rid=row_number() over (partition by dh order by id)
        from t1
    )select a.id,a.name,a.age,b.id,b.class
    from AcHerat a left join t2 b on a.dh = b.dh and a.rid = 1
      

  2.   

    select id,name,age,dh into #t1 from t1 order by dh,idselect min(id) id,dh into #t2 from t1 group by dh
     
    select a.id,a.name,a.age,isnull(c.id,'') as cid,isnull(c.class,'') as class
    from #t1 a
    left join #t2 b on a.id=b.id
    left join t2 c on b.dh=c.dh 
    临时表实现
      

  3.   

    我有2个表,是通过dh(单号)连接在一起的,如下:create table t1(id int,name varchar(8),age int,dh varchar(3))
    insert t1
    select 1 ,'小王', 22 ,'101' union all
    select 2 ,'小李', 23 ,'101' union all
    select 3 ,'小赵', 24 ,'102' union all
    select 4 ,'小黑', 26 ,'102'
    create table t2(dh varchar(3),id int,class varchar(12))
    insert t2
    select '101', 1 ,'1年1班' union all
    select '102', 2 ,'2年1班' 
    select t1.id,t1.name,t1.age,
    (case when t1.id=(select min(id) from t1 a where a.dh=t1.dh) then t2.id end) [id],
    (case when t1.id=(select min(id) from t1 a where a.dh=t1.dh) then t2.class end) class
    from t1 ,t2 where t1.dh=t2.dh
    drop table t1,t2
    /*
    id          name     age         id          class
    ----------- -------- ----------- ----------- ------------
    1           小王       22          1           1年1班
    2           小李       23          NULL        NULL
    3           小赵       24          2           2年1班
    4           小黑       26          NULL        NULL(4 行受影响)
    */
      

  4.   

    create table t1(id nvarchar(20),name nvarchar(20),age int,dh nvarchar(20))
    create table t2 (dh nvarchar(20),id nvarchar(20),class nvarchar(20))
    insert into t1 (id,name,age,dh) select '1', '小王' ,22, '101' union
    select '2', '小李', 23, '101' union
    select '3', '小赵', 24, '102' union
    select '4', '小黑', 26, '102'
    insert into t2(dh,id,class) select '101', '1', '1年1班' union
    select '102' ,'2','2年1班'select t1.id,t1.name,t1.age,
    (case when t1.id=(select min(id) from t1 a where a.dh=t1.dh) then t2.id else '...' end) [id],
    (case when t1.id=(select min(id) from t1 a where a.dh=t1.dh) then t2.class else '...' end) class
    from t1 ,t2 where t1.dh=t2.dhdrop table t1,t2
      

  5.   

    create table t1(id nvarchar(20),name nvarchar(20),age int,dh nvarchar(20))
    create table t2 (dh nvarchar(20),id nvarchar(20),class nvarchar(20))
    insert into t1 (id,name,age,dh) select '1', '小王' ,22, '101' union
    select '2', '小李', 23, '101' union
    select '3', '小赵', 24, '102' union
    select '4', '小黑', 26, '102'
    insert into t2(dh,id,class) select '101', '1', '1年1班' union
    select '102' ,'2','2年1班'select t1.id,t1.name,t1.age,
    (case when t1.id=(select min(id) from t1 a where a.dh=t1.dh) then t2.id else '...' end) [id],
    (case when t1.id=(select min(id) from t1 a where a.dh=t1.dh) then t2.class else '...' end) class
    from t1 ,t2 where t1.dh=t2.dhdrop table t1,t2