- 论坛徽章:
- 0
|
gamedb1.accountlist gamedb2.accountlist 这2个表在不同的库中,结构完全一样;
表中每行存储一个用户的数据
数据需求是统计这个表中等级分布:就用到一个字段 level
mysql> select level,count(level) num from gamedb1.accountlist group by level;
+-------+--------+
| level | num |
+-------+--------+
| 0 | 1 |
| 1 | 1 |
| 2 | 3 |
+-------+--------+
mysql> select level,count(level) num from gamedb2.accountlist group by level;
+-------+--------+
| level | num |
+-------+--------+
| 0 | 2 |
| 1 | 3 |
| 2 | 4 |
+-------+--------+
然后想把这些合并起来写了个
mysql> select tg.level,tg.num from (select level,count(level) num from gamedb1.accountlist group by level union all select level,count(level) num from gamedb2.accountlist group by level) tg;
+-------+--------+
| level | num |
+-------+--------+
| 0 | 1 |
| 1 | 1 |
| 2 | 3 |
| 0 | 2 |
| 1 | 3 |
| 2 | 4 |
+-------+--------+
但是这样还是不符合数据需求,本人新手搞不大来了,求帮助
理想中的显示结果是:把1和2中的num相加
+-------+--------+
| level | num |
+-------+--------+
| 0 | 3 |
| 1 | 4 |
| 2 | 7 |
+-------+--------+ |
|