合并多张数据库表后查询(联合查询)- union

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 可以实现相同结构的表的合并。

猜你喜欢

转载自blog.csdn.net/lx1848/article/details/77619832