1. 有幾張表結構完全一樣的表,我要對這幾張表按某個字段彙總。
參考博客:http://blog.csdn.net/vinson0526/article/details/9367469
比如表upc1:
application | count |
---|---|
http | 3 |
ftp | 4 |
www | 10 |
表upc2:
application | count |
---|---|
http | 7 |
ftp | 6 |
www | 13 |
最終想得到如下表tt:
application | count |
---|---|
http | 10 |
ftp | 10 |
www | 23 |
代碼:
select application, sum(count) from (
select * from upc1
union all
select * from upc2) tt
group by applicaton;
總結: union 和 union all 可以實現相同結構的表的合併。