比如说,有A表和B表。两个表结构都一样,A表保存2个月内的数据,B表保存2个月前的数据,现在我想以A表字段1为条件查询,也就是说再A表中没有就在B表中查,请教各位高手,SQl语句应该怎么写。

解决方案 »

  1.   

    select * from a where col1='aa'
    union all
    select * from b where col1='aa'
      

  2.   

    [code=SQL]
    select * from 
    (select * from a 
    union 
    select * from b ) A
    where A.col1 =xxx[/code
      

  3.   


    select * from 
    (select * from a
      union all
    select * from b) tb
    where a1>0
      

  4.   

    如果B表数据量不大就用union all同时查,数据量大就先判断A表是否有返回值再查询B表
      

  5.   


    if exist (select * from A where 条件)
     begin
       查询A表
      end
    else
      begin
       查询B表
      end这样执行效率会比用union好些