select count(name) as number, office
from
(
select * from a
union
select * from b
) t
group by office

解决方案 »

  1.   

    select sum(1) as number, office
    from
    (
    select * from a
    union all
    select * from b
    ) t
    group by office
      

  2.   

    select count(*) number,office from ( select * from a union select * from b) t group by office或:select sum(1) number,office from ( select * from a union select * from b) t group by office或:select count(distinct name) number,office from ( select * from a union all select * from b) t group by office
      

  3.   

    思归的办法是对的,应该用union,用union all会有重复的记录
      

  4.   

    如果用union all的话可以用count(distinct name)
      

  5.   

    谢谢大家了,用了思归的方法,终于可以睡觉了......
    还有个小问题:
    为什么不能这样?
    select count(name) as number, office
    from
    (
    (select * from a) t1
    union
    (select * from b) t2
    ) t
    group by office不过,先结帐了.
      

  6.   

    the syntax is wrong, you meanselect count(name) as number, office
    from
    (
    select * from a as t1
    union
    select * from b as t2
    ) t
    group by office?