0

2つのテーブルがあります。表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 に答える