gpt4 book ai didi

来自多个表的 mysql count(*)

转载 作者:可可西里 更新时间:2023-11-01 06:34:36 26 4
gpt4 key购买 nike

我有一个 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)`

我的问题是如何生成这两个结果的总和,而不必在我的应用程序代码中这样做。

最佳答案

Tyler 可能是对的,设计肯定是错误的!

但这是可能的..

select 
sum(a.count)
from
(select count(*) as count from table1
union all
select count(*) as count from table2) a

关于来自多个表的 mysql count(*),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4777066/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com