我有一个MYSQL数据库,其中包含我公司客户每天使用我们服务的数据。
数据库中的每个表代表一个24小时的时间段以及在该时间段内发生的所有客户交易。
我想计算好几天的总交易次数
到目前为止,我有一个select语句执行以下操作
select
(
select count(customer) from 2010Dec28
where customer='<customer name>'
) as t1,
(
select count(customer) from 2010Dec29
where customer='<customer name>'
)as t2;`
但是这会将结果作为两个单独的计数
返回| t1 | t2 |
| 1438 | 16282 |
1 row n set (0.00 sec)`
我的问题是如何在我的应用程序代码中生成这两个结果的总和。
答案 0 :(得分:12)
但它有可能......
select
sum(a.count)
from
(select count(*) as count from table1
union all
select count(*) as count from table2) a
答案 1 :(得分:10)
修复您的数据库设计。除非你在数十亿行上进行某种海量数据存储,否则每天一个单独的表是非常不合适的。
答案 2 :(得分:4)
只需将交叉连接换成直接子+ sub,如下所示。比制作任何工会或加入要容易得多
select
(
select count(customer) from 2010Dec28
where customer='<customer name>'
) + (
select count(customer) from 2010Dec29
where customer='<customer name>'
)