A表数据
AID  AName
1    car
2    book
3    penB表数据
BID  AID  BName
1    1    auto
2    2    bo
3    2    bo2
4    1    bus要得到
AID  AName  BName
1    car    auto
2    book   bo
3    pen    <null>sql语句怎么写?在access中写法又是如何?求教

解决方案 »

  1.   

    select a.AID,a.AName,b.BName from A表 a left join B表 b on a.AID=b.AID
      

  2.   

    create table a(AID int,AName varchar(10))
    insert a select 1,'car'
    union all select 2,'book'
    union all select 3,'pen'create table b(BID int,AID int,BName varchar(10))
    insert b select 1,1,'auto'
    union all select 2,2,'bo'
    union all select 3,2,'bo2'
    union all select 4,1,'bus'
    select a.*,c.bname from a left join (select * from b where bid in(select min(bid) from b group by aid)) c
    on a.aid=c.aidselect a.*,c.bname from a left join 
    (select * from b t where exists(select 1 from b where aid=t.aid and bid>t.bid)) c
    on a.aid=c.aid
      

  3.   

    select a.*,c.bname from a left join 
    (select * from b t where not exists(select 1 from b where aid=t.aid and bid<t.bid)) c
    on a.aid=c.aid