select a.字段1,a2.字段2 from tab1 a,tab2 a2
如果随便一个表没有数据的话,但是另外一个表有数据,那么查询出来的结果是都没数据的,为什么?我想要的结果是有数据的就显示,没有的不显示,请问SQL语句应该怎么写。

解决方案 »

  1.   


    select 字段1,'' as 字段2 from tab1
    union
    select '' as 字段1,字段2 from tab2
      

  2.   

    select a.字段1,a2.字段2 from tab1 a left join tab2 a2 a.某字段=a2.某字段
      

  3.   

    使用Full Join如:
    select a.字段1,a2.字段2 from tab1 a full join tab2 a2 a.某字段=a2.某字段
      

  4.   


    select 字段1,'' as 字段2 from tab1
    union all
    select '' as 字段1,字段2 from tab2
      

  5.   

    使用完整外部联接:full join 或 full outer join 
    两表相同的组合在一起,A表有,B表没有的数据(显示为null),同样B表有A表没有的显示为(null)
    select a.字段1,a2.字段2 from tab1 a full join tab2 a2 on a.某字段=a2.某字段
    或者
    select a.字段1,a2.字段2 from tab1 a full outer join tab2 a2 on a.某字段=a2.某字段
      

  6.   

    连接查询有你那么写的么。你那种写法完全没什么意义。
    如果是连接查询的话,你没写连接方式,以及where条件,也就相当于查出两个表的数据。
    和分别查两个表没区别。 
    select a.ziduan1 ,a.ziduan2 ,b.ziduan1,b.ziduan2 from a inner join b  
     on a.id=b.a_id  where  a.id>5 上面是标准的了解查询。
    a ,b表间有主外键关系,自己看吧
      

  7.   

    如果B表的数据A表中一定有,如果A表在左边,就用左连接 Left JOIN
    如果B表的数据A表中一定有,如果A表在右边,就用右连接 Right JOIN
    如果A表与B表中数据都可能有,则用全连接  FULL JOIN
    如果A表与B表数据内容全不同,需交叉显示,则用Cross JOIN。
      

  8.   

    select a.字段1,a2.字段2 from tab1 a full join tab2 a2 on a.某字段=a2.某字段 
    内连接
    select a.字段1,a2.字段2 from tab1 a full outer join tab2 a2 on a.某字段=a2.某字段
    外连接
      

  9.   

    用inter join来做吧
    应该能满足楼主的需求