0

Between two dates I need to display "today result".

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');

My query is:

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
4

1 回答 1

0

If I understand your question correctly, I think you just need to filter by current date:

WHERE   a.gid ='1'
AND DATE(a.ACCESSTIME) = CURDATE()
GROUP BY a.gid, a.qid;

or you can use this, that could be faster because is able to make use of an index:

WHERE   a.gid ='1'
AND a.ACCESSTIME >= CURDATE() AND a.ACCESSTIME < CURDATE() + INTERVAL 1 DAY
GROUP BY a.gid, a.qid;
于 2013-03-30T08:47:09.537 回答