表1(Table1)如下:
--------------------
Name NameId
-----------------------
张三  1
李四  2
王五  3表2(Table2)如下:
--------------------
NameId  abc
-------------
2     80如何关联查询得出以下记录:
Name  NameId  abc
----------------------
张三  1      无成绩
李四  2     80
王五  3     无成绩
谢谢各位。

解决方案 »

  1.   

    select a.naem,a.nameid,isnull(cast(b.abc as varchar(10)),'无成绩') as abc
    from table1 a left join table2 b on a.nameid = b.nameid
      

  2.   


    select a.*,isnull(b.abc,N'无成绩')
        from T1 a left join T2 b
        on a.name=b.name
      

  3.   

    SELECT T1.NAME,T1.NAMEID,ISNULL(LTRIM(T2.ABC),'无成绩')
    FROM TABLE1 T1
    LEFT JOIN TABLE2 T2 ON T1.NAMEID=T2.NAMEID
      

  4.   

    select a.name,a.nameid ,b.abc from table1 a ,table2 b where a.nameid=b.nameid
      

  5.   


    select a.*,isnull(b.abc,N'无成绩')  from T1 a,T2 b
      

  6.   

    select *,isnull(abc,'无成绩') from tb1,tb2 where tb1.nameid=tb2=nameid 
      

  7.   

    如果表2,再加一个条件:
    表2(Table2)如下:
    --------------------
    NameId abc  tt
    -------------
    2 80      1
    1 60      1
    1  70     2当我查询第1学期(tt=1)时,得出:
    Name NameId abc tt
    ----------------------
    张三 1 60         1
    李四 2 80        1
    王五 3 无成绩     1查询第2学期(tt=2)时,得出
    Name NameId abc tt
    ----------------------
    张三 1 70         2
    李四 2 无成绩     2
    王五 3 无成绩     2应该如何实现?
      

  8.   

    --try
    --create table select * from table1
    Name NameId
    ---- -----------
    张三   1
    李四   2
    王五   3(3 行受影响)select * from table2
    NameId      abc         tt
    ----------- ----------- -----------
    2           80          1
    1           60          1
    1           70          2(3 行受影响)
    --查询第1学期(tt=1)时,得出:DECLARE @a int = 1  ----查询第1学期(tt=1)
    select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
    from table1 a
    left join table2 b on a.NameId=b.NameId where b.tt = @a
    Union all
    select Name,NameId,'无成绩' abc,@a
    from table1 a
    where not exists (select * from table2 where a.NameId = NameId and tt = @a )--result:
    Name NameId      abc        tt
    ---- ----------- ---------- -----------
    张三   1           60         1
    李四   2           80         1
    王五   3           无成绩        1(3 行受影响)
    --查询第2学期(tt=2)时,得出
    DECLARE @a int = 2  ----查询第1学期(tt=2)
    select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
    from table1 a
    left join table2 b on a.NameId=b.NameId where b.tt = @a
    Union all
    select Name,NameId,'无成绩' abc,@a
    from table1 a
    where not exists (select * from table2 where a.NameId = NameId and tt = @a )--result:
    Name NameId      abc        tt
    ---- ----------- ---------- -----------
    张三   1           70         2
    李四   2           无成绩        2
    王五   3           无成绩        2(3 行受影响)
      

  9.   

    --修改left join 为INNER JOIN  
    --查询第1学期(tt=1)时,得出:DECLARE @a int = 1  ----查询第1学期(tt=1)
    select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
    from table1 a
    join table2 b on a.NameId=b.NameId where b.tt = @a
    Union all
    select Name,NameId,'无成绩' abc,@a
    from table1 a
    where not exists (select * from table2 where a.NameId = NameId and tt = @a )--result:
    Name NameId      abc        tt
    ---- ----------- ---------- -----------
    张三   1           60         1
    李四   2           80         1
    王五   3           无成绩        1(3 行受影响)
    --查询第2学期(tt=2)时,得出
    DECLARE @a int = 2  ----查询第1学期(tt=2)
    select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
    from table1 a
    join table2 b on a.NameId=b.NameId where b.tt = @a
    Union all
    select Name,NameId,'无成绩' abc,@a
    from table1 a
    where not exists (select * from table2 where a.NameId = NameId and tt = @a )--result:
    Name NameId      abc        tt
    ---- ----------- ---------- -----------
    张三   1           70         2
    李四   2           无成绩        2
    王五   3           无成绩        2(3 行受影响)
      

  10.   

    就这个:
    -------------------------------------------
    DECLARE @a int = 2  ----查询第1学期(tt=2)
    select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
    from table1 a
    join table2 b on a.NameId=b.NameId where b.tt = @a
    Union all
    select Name,NameId,'无成绩' abc,@a
    from table1 a
    where not exists (select * from table2 where a.NameId = NameId and tt = @a )
      

  11.   

    DECLARE @a int = 2 ----查询第1学期(tt=2)
    select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
    from table1 a
    join table2 b on a.NameId=b.NameId where b.tt = @a
    Union all
    select Name,NameId,'无成绩' abc,@a
    from table1 a
    where not exists (select * from table2 where a.NameId = NameId and tt = @a )
      

  12.   

    DECLARE @a int = 2 ----查询第1学期(tt=2)
    select a.Name,a.NameId,cast(b.abc as varchar(10)) abc,b.tt
    from table1 a
    join table2 b on a.NameId=b.NameId where b.tt = @a
    Union all
    select Name,NameId,'无成绩' abc,@a
    from table1 a
    where not exists (select * from table2 where a.NameId = NameId and tt = @a )