2013-03-30 86 views
0

兩個日期之間我需要顯示「今日結果」。我需要顯示「今日結果」

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 
+1

請澄清你的問題。你的查詢做錯了什麼? – Barmar

+0

我幾乎認爲僅僅基於拼寫錯誤的數量來調高這一個:-) – sbrattla

+0

你想要的是什麼upvote? –

回答

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; 
+0

謝謝您先生。爲我工作...... :) –