A表里有30条数据。
B表里有40条数据。如何用sql语句算出共有70条语句。我用
select count(a1.*)+count(a2.*) 总数 from a1,a2.这样统计出来的数目,比实际大很多。

解决方案 »

  1.   

    --你这样统计是40*30条(笛卡儿积)
    --这样
    select (select count(*) from a1)+(select count(*) from a2) from dual;
      

  2.   

    在2个表中找一个列 类型一致的,比如a 表中col_a和b表中col_b类型一致
    select count(*) from (select col_a from a
                          union all
                          select col_b from b) x
      

  3.   

    ----如果A、B数据表结构相同的话
    ;with  as c(select * from A
    union all
    select * from B
    )
    select count(*) from c
    ----对于A、B表结构不相同的话
    select (select count(a1.*) from A)+(select count(a2.*) from B) 
    from dual
      

  4.   


    SQL> select count(*) from emp;  COUNT(*)
    ----------
            14SQL> select count(*) from dept;  COUNT(*)
    ----------
             4SQL> select
      2  (select count(*) from emp)+
      3  (select count(*) from dept)
      4  sum
      5  from dual;       SUM
    ----------
            18
      

  5.   

    select sum(count1) 
    from (
    select count(*) count1 from a1
    union
    select count(*) count1 from a2
    )