我在一个数据库中有多个MySQL表,每个表记录执行的事务。每个表都有一个DATETIME字段,用于记录每个事务发生的时间。
如何构建MySQL查询或过程来生成每个表的事务计数,按每个日期分组?
我希望看到从当月开始到当天的每天总数。
在每个表中,一行代表一个事务。因此,对于每个日期,列只是每个表中的行数。
运行这样的查询,今天,会产生一个像;
这样的表26/04/2016 Total A Transactions,Total B Transactions,...
25/04/2016 Total A Transactions,Total B Transactions,...
...
01/04/2016 Total A Transactions,Total B Transactions,...
示例模式;
CREATE TABLE tableA
(
uuid
BIGINT(20)UNSIGNED NOT NULL AUTO_INCREMENT,
creationTime
DATETIME NOT NULL,..
CREATE TABLE tableB
(
uuid
BIGINT(20)UNSIGNED NOT NULL AUTO_INCREMENT,
creationTime
DATETIME NOT NULL,..
...
答案 0 :(得分:2)
select date(my_date), count(*), 'tableA' fromc tableA
where month(my_date) = month(curdate())
AND year(my_date) = Year(curdate())
group by date(my_date)
union
select date(my_date), count(*), 'tableB' fromc tableB
where month(my_date) = month(curdate())
AND year(my_date) = Year(curdate())
group by date(my_date)
答案 1 :(得分:1)
我认为tableA每天至少有一笔交易。
select create_time, total_a_transaction, total_b_transaction, ...
from
(select date(creationTime) create_time, count(*) total_a_transaction
from tableA
where year(creationTime) = year(curdate())
and month(creationTime) = month(curdate())
group by date(creationTime)) transaction_a
left join
(select date(creationTime) create_time, count(*) total_b_transaction
from tableB
where year(creationTime) = year(curdate())
and month(creationTime) = month(curdate())
group by date(creationTime)) transaction_b
using (create_time)
left join ...