2013-04-05 148 views
-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 

問:在給定結果集下使用的ineed結果2013-04-02至2013-04-05工作正常但當天2013-04-05至2013-04-05沒有得到結果?任何想法請 這裏是sqlfidler:mysql查詢內連接結果集問題

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

 

------------------------ 
qtext  1st attempt correct  1st attempt incorrect all attempt correct all attempt incorrect 
----------  ---------------------  ---------------------- -------------------- --------------------------- 
question  0   0    0    1 
question  0   0    0    1 
question  0   0    0    0 
question  0   0    0    0 
question  0   0    1    0 

回答

0

這是你在找什麼 - 移動WHERE標準的JOIN。上述查詢的問題是您正在加入MIN(AccessTime),並且在MIN結果中沒有記錄在2013-04-05回來。

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 

Updated SQL Fiddle

+0

是好的,但我需要first_attempt_correct,first_attempt_incorrect我們需要爲上述嘗試顯示0 – 2013-04-05 05:05:54