两张表 a,b
a中编号  姓名  性别
 2    'x'   '男'b中编号  年级
 3    '2'请问如何通过编号='2',查询a,b两表,得到以下数据编号  姓名  性别       年级
 2    'x'   '男'     null

解决方案 »

  1.   

    select 编号,姓名,性别,年级 from a left join b on a.编号=b.编号 where 编号='2'
      

  2.   

    declare @a table (bh char(10),xm char(10),xb char(10))
    insert into @a
    values (2,'x','男')
    declare @b table (bh char(10),nj char(10))
    insert into @b
    values (3,2)select a.bh,xm,xb,nj from @a  a left join @b b on a.bh=b.bh where a.bh='2'
      

  3.   


    declare @ta table(ID int ,Name varchar(10))
    insert into @ta select 1,'a'
    insert into @ta select 2,'b'
    declare @tb table(code int)
    insert into @tb select 2
    insert into @tb select 3select a.* ,b.* from @ta a left join @tb b on a.ID=b.code
    /*
    ID          Name       code
    ----------- ---------- -----------
    1           a          NULL
    2           b          2
    */
      

  4.   


    select * from (select 编号,姓名,性别,年级 from a left join b on a.编号=b.编号 where 编号='2')d left join c on c.id=d.id无论有多少张表都可以一直左连接下去哦
      

  5.   


    select * from a left join b on a.pkcol=b.pkcol left join c on a.pkcol=c.pkcol