select x.*.y.* from (select a,b from t) x,(select a,b from t2)y
按照上面的写法,当y表中无记录x表中有记录 或 y表中有记录x表中无记录时,整个语句不返回值,也就是说只要其中一个表无记录整个语句就不返回值。现在的想只要x表或y表其中任何表存在记录时,就要显示字段出来,语句该怎么写?

解决方案 »

  1.   

    --改为
    select a,b from t
    Union 
    select a,b from t2
      

  2.   

    用两句比较容易解决
    select x.* .y.*
      from (select a, b from t) x
      left join (select a, b from t2) y on x.a = y.a
     where (select count(*) from (select a, b from t)) >
           (select count(*) from select a, b from t2
            ));select x.* .y.*
      from (select a, b from t) x
      right join (select a, b from t2) y on x.a = y.a
     where (select count(*) from (select a, b from t)) <
           (select count(*) from select a, b from t2
            ));
      

  3.   

    用union的话一个烦,第二影响速度。
      

  4.   

    union 是最简洁而且最方便的方法,如果你用其他的方法可能效率还比不上union ...
      

  5.   

    to mantisXF:
    select x.*.y.* from (select a,b from t) x,(select a,b from t2)y
    正常情况下得到 x.a,x.b,y.a,y.b如果用union,请问怎么写?
      

  6.   

    用full outer join on 1=1来解决。
      

  7.   

    SQL> select * from t;A                    B
    -------------------- --------------------
    a                    bSQL> select * from t2;未选定行SQL> select x.*,y.* from 
      2  (select a,b from t) x full outer join 
      3  (select a,b from t2) y on 1=1;A                    B                    A
    -------------------- -------------------- --------------------
    B
    --------------------
    a                    b
      

  8.   

    select x.* , y.* 
    from (select * from  t1) x
          full join (select * t2)y
       on 1 = 1OK?
      

  9.   

    select distinct  x.* , y.* 
    from (select * from  t1) x
          full join (select * t2)y
       on 1 = 1
      

  10.   

    直接使用full outer join 就 OK!
      

  11.   

    直接使用full outer join