在两个日期之间我需要显示"今天结果"。
CREATE TABLE IF NOT EXISTS `maf_game_stats` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` varchar(100) NOT NULL,
`sessionid` varchar(100) NOT NULL,
`gid` int(11) NOT NULL,
`qid` int(11) NOT NULL,
`result` varchar(50) NOT NULL,
`accesstime` datetime NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `maf_game_stats` (`id`, `userid`, `sessionid`, `gid`, `qid`, `result`, `accesstime`) VALUES
(1, 'FYEJHQWSHXMV263', '35sd797n57plmreb5ecssuhhh2', 1, 5, '0', '2013-03-30 13:34:03'),
(2, 'FYEJHQWSHXMV263', '35sd797n57plmreb5ecssuhhh2', 1, 1, '0', '2013-03-30 13:34:07'),
(3, 'FYEJHQWSHXMV263', '35sd797n57plmreb5ecssuhhh2', 1, 10, '1', '2013-03-30 13:34:12'),
(4, 'FYEJHQWSHXMV263', '35sd797n57plmreb5ecssuhhh2', 1, 2, '1', '2013-03-30 13:34:17'),
(5, 'FYEJHQWSHXMV263', '35sd797n57plmreb5ecssuhhh2', 1, 16, '1', '2013-03-30 13:34:23');
我的查询是:
SELECT a.gid, a.qid, SUM(a.result = 1) correct, SUM(a.result = 0) incorrect FROM maf_game_stats a
INNER JOIN
(
SELECT USERID, gid, QID, MIN(ACCESSTIME) min_date
FROM maf_game_stats
GROUP BY USERID, gid, qid
) b ON a.USERID = b.USERID AND
a.gid = b.gid AND
a.qid = b.qid AND
a.ACCESSTIME = b.min_date
WHERE a.gid ='1'
AND a.ACCESSTIME BETWEEN CAST('2013-03-29' AS DATETIME) AND CAST('2013-03-30' AS DATETIME)
GROUP BY a.gid, a.qid
答案 0 :(得分:0)
如果我理解你的问题,我认为你只需按当前日期过滤:
WHERE a.gid ='1'
AND DATE(a.ACCESSTIME) = CURDATE()
GROUP BY a.gid, a.qid;
或者你可以使用它,因为它可以更快,因为它能够使用索引:
WHERE a.gid ='1'
AND a.ACCESSTIME >= CURDATE() AND a.ACCESSTIME < CURDATE() + INTERVAL 1 DAY
GROUP BY a.gid, a.qid;