我有一个活动供稿,想要查询我的表格以抓取用户的所有Feed项目,同时将它们分组为2天的间隔。
因此,一个Feed项可能会说“用户在2天前完成了这些操作”,它会显示这些操作的Feed项ID。
我的问题是我无法弄清楚如何将每个Feed项目放入与2天间隔相关联的组中。
现在我有45个结果,全部都在整个月内。我能做的最好的事情就是做group by
,我把它们分成两天。但我需要在结果中获取Feed id而不是计数。
例如
dayGroup | feedId
-------------------
1 | 300
1 | 301
2 | 400
2 | 401
伪pql中的
select feedId from feeds where userid = 1 and feedType = "action" group by every 2 days
关于当天分组的进一步说明。
如果在08-07-15创建了一个Feed项,那么从最新的Feed活动向后看2天到08-05-15作为dayGroup,依此类推,直到没有更多的Feed项为止。根据活动供稿的实际最新日期,这将是动态的。
我的表格看起来像这样
CREATE TABLE `activity_feed` (
`feed_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`text` varchar(255) DEFAULT NULL,
`data` varchar(255) DEFAULT NULL,
`status` tinyint(1) DEFAULT '0',
`date_created` datetime DEFAULT NULL,
`type` varchar(55) DEFAULT NULL,
`section` varchar(255) DEFAULT NULL,
`data_user_id1` int(11) DEFAULT '0',
`data_user_id2` int(11) DEFAULT '0',
`data_game_id` int(11) DEFAULT '0',
`layout` tinyint(1) DEFAULT '0',
`first_feed_id` int(11) DEFAULT '0',
PRIMARY KEY (`feed_id`),
KEY `idx1` (`user_id`) USING BTREE,
KEY `idx2` (`section`) USING BTREE,
CONSTRAINT `af_fk1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1838 DEFAULT CHARSET=latin1;
答案 0 :(得分:0)
create table dtGrpTwo
( gno int not null, -- group number
dom int not null -- day of month
);
insert dtGrpTwo (gno,dom) values (1,1),(1,2),(2,3),(2,4),(3,5),(3,6),(4,7),(4,8); -- etc up to group 16
create table evts
(
feed_id int auto_increment primary key,
user_id int not null,
feedType varchar(55) not null,
date_created datetime not null
);
-- truncate table evts;
insert evts (user_id,feedType,date_created) values (1001,'ACTION','2014-11-01');
insert evts (user_id,feedType,date_created) values (1001,'ACTION','2014-11-02');
insert evts (user_id,feedType,date_created) values (1001,'PULSE','2014-11-02');
insert evts (user_id,feedType,date_created) values (1001,'ACTION','2014-11-02');
insert evts (user_id,feedType,date_created) values (1001,'ACTION','2014-11-02');
insert evts (user_id,feedType,date_created) values (1001,'ACTION','2014-11-05');
insert evts (user_id,feedType,date_created) values (1001,'PULSE','2014-11-05');
insert evts (user_id,feedType,date_created) values (1001,'ACTION','2014-11-08');
insert evts (user_id,feedType,date_created) values (1001,'ACTION','2014-12-08');
insert evts (user_id,feedType,date_created) values (1001,'ACTION','2014-12-08');
insert evts (user_id,feedType,date_created) values (1001,'ACTION','2014-12-08');
insert evts (user_id,feedType,date_created) values (666,'ACTION','2014-11-01');
insert evts (user_id,feedType,date_created) values (666,'ACTION','2014-11-02');
insert evts (user_id,feedType,date_created) values (666,'ACTION','2014-11-02');
select g.gno as dayGroup,e.feed_id
from dtGrpTwo g
join evts e
on dayofmonth(e.date_created)=g.dom
and e.user_id=1001
and feedType='ACTION'
and e.date_created between '2014-11-01' and '2014-11-30'
order by g.gno,e.feed_id
+----------+---------+
| dayGroup | feed_id |
+----------+---------+
| 1 | 1 |
| 1 | 2 |
| 1 | 4 |
| 1 | 5 |
| 3 | 6 |
| 4 | 8 |
+----------+---------+
6 rows in set (0.01 sec)