0

好的,我有两个表:表 1 如下所示:

id age gender
1   10   M
2   11   F
3   11   F

表 2 看起来像这样(不同的值相同):

id age gender
1  11   F
2  12   M
3  10   M

现在我希望我的最终输出如下所示:

age count
10   2
11   3
12   1

实现这一目标的最有效方法是什么?

4

2 回答 2

6

您要聚合联合:

select age, count(*)
from (select id, age, gender from table1 union all
      select id, age, gender from table2
     ) t
group by age
于 2013-01-31T22:13:10.060 回答
0

尝试这个

select age ,count(age) count from table1 group by age
union
select age, count(age)  count from table2 group by age
于 2013-01-31T22:09:20.030 回答