有两张表,结构如下:表A:
ID(主键)   Title    Content 
a001        aaaa     aaaaaa
a002        bbbb     bbbbbb
……表B:
ID    Title    Content  AID(该字段对应表A中的ID) 
b001  xxxx     xxxxxx   a001
b002  yyyy     yyyyyy   a001
b003  zzzz     zzzzzz   a002现在我想要根据A中的ID(如:a001) 得到如下的数据:a001   aaaa   aaaaaa
b001   xxxx   xxxxxx
b002   yyyy   yyyyyy 请问这个SQL语句怎么写???

解决方案 »

  1.   

    select * from A where ID = 'a001'
    union all
    select ID,Title,Content from B where AID = 'a001' 
      

  2.   

    Select A.id,A.title,A.Content From A Where A.Id='a001'
    union
    Select B.id,B.title,B.content From b where b.Aid='a001'
      

  3.   

    union 查询出的结果排除重复的数据
    union all 查询出的结果不排除重复的数据
      

  4.   

    select distinct  a.* from @A a, @B b where a.ID=b.Aid
    union all 
    select distinct b.ID,b.Title,b.Content from @A a, @B b where a.ID=b.Aid
      

  5.   

    select * from 表A where ID='a001'
    union all
    select ID,title,content From 表B where AID='a001'