请看下面两表
表1 ABC(这有5个记录)
ABC_ID     ABC_NID
1          S1
2          S3
3                   (没内容)
4          S4
5          S2
--------------------------
表2 NE(这有4个记录)
NE_ID      NE_NA
S1         小明
S2         小李
S3         小红
S4         小张
--------------------------
我要显示的结果:
1           小明
2          小红
3
4          小张
5          小李
--------------------------
查询语句应该怎样写??

解决方案 »

  1.   

    create table ABC(
    ABC_ID int,
    ABC_NID varchar(10)
    )insert ABC select 1,'S1'
    union all select 2,'S3'
    union all select 3,null
    union all select 4,'S4'
    union all select 5,'S2'create table NE(
    NE_ID varchar(10),
    NE_NA varchar(10)
    )insert NE select 'S1','小明'
    union all select 'S2','小李'
    union all select 'S3','小红'
    union all select 'S4','小张'select a.ABC_ID,isnull(b.NE_NA,'') from ABC a left join NE b on a.ABC_NID=b.NE_IDdrop table ABC,NE
      

  2.   

     declare @T table(ABC_ID int,ABC_NID varchar(10))
    insert into @T select 1,'S1'
    union all select 2,'S3'
    union all select 3,''
    union all select 4,'S4'
    union all select 5,'S2'declare @B table(NE_ID varchar(10),NE_NA varchar(10))
    insert into @B select 'S1','小明'
    union all select 'S2','小李 '
    union all select 'S3','小红'
    union all select 'S4','小张'
     
    select a.abc_id, b.ne_na from @T a,@B b where a.abc_nid*=b.ne_id
      

  3.   

    使用LEFT JOIN 或者RIGHT JOIN,就可以了