两表查询,每个表大约有3万条记录,有可能第一个表有的记录第二个表没有,如何写能效率比较高。
       a表                b表
    1     aaa                              1    aaa
      2     bbb                              4    ddd
      3     ccc
  想得到的结果是:
       1: aaa
       2: bbb
       3: ccc
       4: ddd

解决方案 »

  1.   

    select distinct c.* from (select a.* from a union select b.* from b) c
      

  2.   

    select a.* from a union select b.* from b where b.id not in(select a.id from a)试试这个呢
      

  3.   

    要提高效率的话,改数据库结构、加索引
    目前1     aaa 这样的数据在两个表里面都有,应该合并
      

  4.   

    如果单纯的记录重复使用union即可去掉重复记录
    select a.* from a 
    union 
    select b.* from b
      

  5.   

    同意楼上的,用Union,可以自动过滤掉重复记录,如果不要自动过滤的话,就用Union All
      

  6.   

    还不如直接用 cross join-_-
      

  7.   

    使用
    select a.* from a union select b.* from b where b.id not in(select a.id from a)
    需要过滤重复的数据
    select a.* from a union all select b.* from b where b.id not in(select a.id from a)
    如果用union all 则SQL运行是单个运行然后把结果合并处理.这样不需要排序.