2013-04-05 6 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 

Q : 다음 결과 집합 사용 결과 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

당신이 찾고있는 무슨이이다 - 당신의 JOINWHERE 기준을 이동합니다. 위 질문에 대한 문제는 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_incorrect 우리가 위의 시도에 대해 0을 표시해야, first_attempt_correct 필요 –