-3
CREATE TABLE IF NOT EXISTS `maf_game_questions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `gid` int(11) NOT NULL,
  `qid` int(11) NOT NULL,
  `qtext` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB ;


INSERT INTO `maf_game_questions` (`id`, `gid`, `qid`, `qtext`) VALUES
(1, 1, 6, 'Click on a state that has part of the Rocky Mountain Range to reveal a fact about agriculture.'),
(2, 1, 1, 'Click on a state that borders another country to see a fact about agriculture.'),
(3, 1, 15, 'Find a state where part of the border is created by a river to show a fact about agriculture.'),
(4, 1, 14, 'Choose a state that has part of the Missouri River to see a fact about agriculture.'),
(5, 1, 5, 'Click on a state that borders the Mississippi river to see a fact about agriculture.'),
(6, 1, 16, 'Click on a state with a panhandle to see a fact about agriculture.'),
(7, 1, 8, 'Find a state that is part of the Great Plains to check out a fact about agriculture.'),
(8, 1, 3, 'Select a state that has a bay to show a fact about agriculture.'),
(9, 1, 13, 'Select a state that has part of the Ohio River to view a fact about agriculture.'),
(10, 1, 9, 'Choose a state that is on the Atlantic Ocean to reveal a fact about agriculture.'),
(11, 1, 10, 'Select a state on the Pacific Ocean to see a fact about agriculture.'),
(12, 1, 4, 'Choose a state on the Gulf of Mexico to show a fact about agriculture.'),
(13, 1, 7, 'Find a state that has part of the Appalachian Mountain Range to see a fact about agriculture.'),
(14, 1, 2, 'Find a state that has a peninsula to check out a fact about agriculture.'),
(15, 1, 11, 'Click on a state with a desert to show a fact about agriculture.'),
(16, 1, 12, 'Find a state that borders one of the Great Lakes to see a fact about agriculture.');


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`)
) ENGINE=InnoDB;


INSERT INTO `maf_game_stats` (`id`, `userid`, `sessionid`, `gid`, `qid`, `result`, `accesstime`) VALUES
(1, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 6, '0', '2013-04-02 16:26:22'),
(2, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 1, '1', '2013-04-02 16:26:27'),
(3, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 15, '0', '2013-04-02 16:26:35'),
(4, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 14, '1', '2013-04-02 16:26:42'),
(5, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 5, '1', '2013-04-02 16:26:51'),
(6, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 6, '1', '2013-04-04 16:27:24'),
(7, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 16, '1', '2013-04-04 16:27:32'),
(8, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 15, '0', '2013-04-04 16:27:38'),
(9, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 8, '1', '2013-04-04 16:27:48'),
(10, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 3, '1', '2013-04-02 16:31:22'),
(11, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 16, '1', '2013-04-02 16:31:28'),
(12, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 13, '0', '2013-04-02 16:31:44'),
(13, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 9, '0', '2013-04-02 16:31:53'),
(14, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 8, '1', '2013-04-02 16:32:00'),
(15, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 16, '1', '2013-04-02 16:35:02'),
(16, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 10, '0', '2013-04-02 16:35:11'),
(17, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 4, '0', '2013-04-02 16:35:17'),
(18, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 9, '1', '2013-04-02 16:35:23'),
(19, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 4, '0', '2013-04-02 16:35:29'),
(20, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 7, '0', '2013-04-03 19:06:12'),
(21, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 2, '1', '2013-04-03 19:06:17'),
(22, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 11, '0', '2013-04-03 19:06:29'),
(23, 'KIHYAKQTZLSB975', 'ccb4vm744cttrc8is1uuosf8e0', 1, 12, '0', '2013-04-03 19:06:33'),
(24, 'KIHYAKQTZLSB975', 'r0ci652ln8j1oqhvjd88s0is34', 1, 5, '0', '2013-04-05 09:47:22'),
(25, 'KIHYAKQTZLSB975', 'r0ci652ln8j1oqhvjd88s0is34', 1, 2, '1', '2013-04-05 09:47:28'),
(26, 'KIHYAKQTZLSB975', 'r0ci652ln8j1oqhvjd88s0is34', 1, 14, '0', '2013-04-05 09:47:37'),
(27, 'KIHYAKQTZLSB975', 'r0ci652ln8j1oqhvjd88s0is34', 1, 9, '1', '2013-04-05 09:47:43'),
(28, 'KIHYAKQTZLSB975', 'r0ci652ln8j1oqhvjd88s0is34', 1, 12, '0', '2013-04-05 09:47:50');

SELECT  a.gid, a.qid, q.qtext, SUM(a.result = 1) first_attempt_correct, SUM(a.result = 0) first_attempt_incorrect, c.all_attempt_correct, c.all_attempt_incorrect
             FROM maf_game_stats a
            INNER JOIN
            (
                SELECT USERID, gid, QID, MIN(ACCESSTIME) AS  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
            INNER JOIN
            (
                SELECT gid, QID, SUM(result = 1) AS all_attempt_correct, SUM(result = 0) AS all_attempt_incorrect
                FROM maf_game_stats
                GROUP BY gid, qid
            )  c ON a.gid = c.gid AND
                a.qid = c.qid 
            INNER JOIN maf_game_questions q ON a.qid = q.qid AND a.gid = q.gid
            WHERE   a.gid ='1' 
            AND a.ACCESSTIME >= '2013-04-05' AND a.ACCESSTIME < '2013-04-05' + INTERVAL 1 DAY        
            GROUP BY a.gid, a.qid

问:使用 2013-04-02 到 2013-04-05 的给定结果集下面的结果可以正常工作,但同一天 2013-04-05 到 2013-04-05 没有得到结果?任何想法请在这里是 sqlfidler :

http://sqlfiddle.com/#!2/181a2/1

----------------------
qtext 第一次尝试正确 第一次尝试不正确 所有尝试正确 所有尝试不正确
---------- --------- -------------------- --- -------- ---------------
问题 0 0 0 1
问题 0 0 0 1
问题 0 0 0 0
问题 0 0 0 0
问题 0 0 1 0

4

1 回答 1

0

这是您正在寻找的 - 将WHERE标准移动到您的JOIN. 上述查询的问题是您正在加入,结果中MIN(AccessTime)没有 2013-04-05 的记录返回MIN

SELECT *
FROM maf_game_stats a
    INNER JOIN
        (
            SELECT USERID, gid, QID, MIN(ACCESSTIME) AS  min_date
            FROM maf_game_stats
            WHERE ACCESSTIME >= '2013-04-05' AND ACCESSTIME < '2013-04-06'
            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
    INNER JOIN
        (
            SELECT gid, QID, SUM(result = 1) AS all_attempt_correct, SUM(result = 0) AS all_attempt_incorrect
            FROM maf_game_stats
            GROUP BY gid, qid
         )  c ON a.gid = c.gid AND
             a.qid = c.qid 
    INNER JOIN maf_game_questions q ON a.qid = q.qid AND a.gid = q.gid
WHERE   a.gid ='1' 
GROUP BY a.gid, a.qid

更新的 SQL Fiddle

于 2013-04-05T04:49:01.230 回答