你相信我有解决办法吗?
我真的不得不在这个上发挥我的想象力。我稍微更改了查询以每天执行 SUM。请跟随:
您基本上需要构建此查询
select user,
SUM(if(purchase_date=DATE(NOW()) - interval 1 day, revenue, 0)) sum1,
SUM(if(purchase_date=DATE(NOW()) - interval 2 day, revenue, 0)) sum2,
SUM(if(purchase_date=DATE(NOW()) - interval 3 day, revenue, 0)) sum3
from purchases
GROUP BY user;
从这个
select * from
(select dt,DATE_FORMAT(dt,'%a %c-%e') dh FROM
(select distinct purchase_date dt from purchases) dthdrs) DateHeaders;
首先,让我们制作一些示例数据:
drop database if exists idris;
create database idris;
use idris
create table purchases
(
id int not null auto_increment,
user int,
purchase_date date,
revenue int,
primary key (id)
) ENGINE=MyISAM;
create table dummydata like purchases;
alter table dummydata drop column id;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set user=4;
insert into purchases (user,purchase_date,revenue) select user,purchase_date,revenue from dummydata;
delete from dummydata;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set user=11;
insert into purchases (user,purchase_date,revenue) select user,purchase_date,revenue from dummydata;
delete from dummydata;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set purchase_date = purchase_date - interval 1 day;
insert into dummydata (purchase_date,revenue) values (date(now()),floor(rand() * 100));
update dummydata set user=1;
insert into purchases (user,purchase_date,revenue) select user,purchase_date,revenue from dummydata;
delete from dummydata;
select * from purchases;
运行此之后,我制作了以下数据:
mysql> select * from purchases;
+----+------+---------------+---------+
| id | user | purchase_date | revenue |
+----+------+---------------+---------+
| 1 | 4 | 2012-06-23 | 45 |
| 2 | 4 | 2012-06-24 | 18 |
| 3 | 4 | 2012-06-25 | 55 |
| 4 | 4 | 2012-06-26 | 20 |
| 5 | 11 | 2012-06-23 | 39 |
| 6 | 11 | 2012-06-24 | 32 |
| 7 | 11 | 2012-06-25 | 44 |
| 8 | 11 | 2012-06-26 | 26 |
| 9 | 1 | 2012-06-23 | 99 |
| 10 | 1 | 2012-06-24 | 17 |
| 11 | 1 | 2012-06-25 | 88 |
| 12 | 1 | 2012-06-26 | 91 |
+----+------+---------------+---------+
12 rows in set (0.00 sec)
mysql>
让我们运行我发布的第一个查询
mysql> select user,
-> SUM(if(purchase_date=DATE(NOW()) - interval 1 day, revenue, 0)) sum1,
-> SUM(if(purchase_date=DATE(NOW()) - interval 2 day, revenue, 0)) sum2,
-> SUM(if(purchase_date=DATE(NOW()) - interval 3 day, revenue, 0)) sum3
-> from purchases
-> GROUP BY user;
+------+------+------+------+
| user | sum1 | sum2 | sum3 |
+------+------+------+------+
| 1 | 88 | 17 | 99 |
| 4 | 55 | 18 | 45 |
| 11 | 44 | 32 | 39 |
+------+------+------+------+
3 rows in set (0.00 sec)
mysql>
这是我发布的第二个查询,它将构建标题
mysql> select * from
-> (select dt,DATE_FORMAT(dt,'%a %c-%e') dh FROM
-> (select distinct purchase_date dt from purchases) dthdrs) DateHeaders;
+------------+----------+
| dt | dh |
+------------+----------+
| 2012-06-23 | Sat 6-23 |
| 2012-06-24 | Sun 6-24 |
| 2012-06-25 | Mon 6-25 |
| 2012-06-26 | Tue 6-26 |
+------------+----------+
4 rows in set (0.00 sec)
mysql>
现在让我们组成 VOLTRON
这是您需要的标题组合的两个查询:
mysql> select CONCAT('select user,',rv,' from purchases GROUP BY user')
-> INTO @voltron from
-> (select GROUP_CONCAT(CONCAT('SUM(if(purchase_date=''',dt,''',revenue,0)) as "',dh,'"')) rv
-> from (select dt,DATE_FORMAT(dt,'%a %c-%e') dh FROM
-> (select distinct purchase_date dt from purchases) dthdrs) DateHeaders) A;
Query OK, 1 row affected (0.01 sec)
mysql> select @voltron\G
*************************** 1. row ***************************
@voltron: select user,SUM(if(purchase_date='2012-06-23',revenue,0)) as "Sat 6-23",SUM(if(purchase_date='2012-06-24',revenue,0)) as "Sun 6-24",SUM(if(purchase_date='2012-06-25',revenue,0)) as "Mon 6-25",SUM(if(purchase_date='2012-06-26',revenue,0)) as "Tue 6-26" from purchases GROUP BY user
1 row in set (0.00 sec)
mysql>
好的,我形成了查询。但是有用吗???
看哪
mysql> PREPARE s1 FROM @voltron;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> EXECUTE s1;
+------+----------+----------+----------+----------+
| user | Sat 6-23 | Sun 6-24 | Mon 6-25 | Tue 6-26 |
+------+----------+----------+----------+----------+
| 1 | 99 | 17 | 88 | 91 |
| 4 | 45 | 18 | 55 | 20 |
| 11 | 39 | 32 | 44 | 26 |
+------+----------+----------+----------+----------+
3 rows in set (0.00 sec)
mysql> DEALLOCATE PREPARE s1;
Query OK, 0 rows affected (0.00 sec)
mysql>
所以,这是可能的。您只需发挥您的想象力,让 MySQL 为您构建查询。