I would like to know the number of times each game is played. I need result like this : ------------------------------------------------------ Games Count --------- --------- Ag Across America 5 geam 2 7 game 3 10 Background info: CREATE TABLE IF NOT EXISTS `maf_game` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(250) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; INSERT INTO `maf_game` (`id`, `name`) VALUES (1, 'Ag Across America'); CREATE TABLE IF NOT EXISTS `maf_game_attempts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userid` varchar(100) NOT NULL, `sessionid` varchar(100) NOT NULL, `gid` int(11) NOT NULL, `status` varchar(50) NOT NULL, `attempttime` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ; INSERT INTO `maf_game_attempts` (`id`, `userid`, `sessionid`, `gid`, `status`, `attempttime`) VALUES (1, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'start', '2013-03-25 18:58:14'), (2, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'start', '2013-03-25 18:59:02'), (3, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'start', '2013-03-25 18:59:33'), (4, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'Over', '2013-03-25 19:00:41'), (5, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 'start', '2013-03-25 19:01:38'), (6, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 'start', '2013-03-25 19:01:44'), (7, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 'start', '2013-03-25 19:01:48'), (8, 'AXDUYNBAUIGC268', 'o9u8tno9qvrj8cfja2g1c516f5', 1, 'start', '2013-03-25 19:01:49'), (9, 'ZKHBILGVNJBB040', '8h8r13pnad0ftitsdjt9co49p0', 1, 'start', '2013-03-25 19:01:54'), (10, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 'start', '2013-03-25 19:02:08'), (11, 'AXDUYNBAUIGC268', 'o9u8tno9qvrj8cfja2g1c516f5', 1, 'start', '2013-03-25 19:02:09'), (12, 'ZKHBILGVNJBB040', '8h8r13pnad0ftitsdjt9co49p0', 1, 'start', '2013-03-25 19:02:10'), (13, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 'Over', '2013-03-25 19:02:15'), (14, 'AXDUYNBAUIGC268', 'o9u8tno9qvrj8cfja2g1c516f5', 1, 'Over', '2013-03-25 19:02:18'), (15, 'ZKHBILGVNJBB040', '8h8r13pnad0ftitsdjt9co49p0', 1, 'Done', '2013-03-25 19:02:20'), (16, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'start', '2013-03-25 19:50:30'), (17, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'start', '2013-03-25 19:50:37'), (18, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'start', '2013-03-25 19:50:45'), (19, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'Over', '2013-03-25 19:50:51'), (20, 'JOQHOIGNUFEC945', 'bb57a1nfodomuf0i01pcojs0f2', 1, 'start', '2013-03-25 19:52:58'), (21, 'JOQHOIGNUFEC945', 'bb57a1nfodomuf0i01pcojs0f2', 1, 'start', '2013-03-25 19:53:01'), (22, 'JOQHOIGNUFEC945', 'bb57a1nfodomuf0i01pcojs0f2', 1, 'Over', '2013-03-25 19:53:08'); 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 DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ; INSERT INTO `maf_game_stats` (`id`, `userid`, `sessionid`, `gid`, `qid`, `result`, `accesstime`) VALUES (1, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 16, '0', '2013-03-25 18:58:14'), (2, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 14, '0', '2013-03-25 18:59:02'), (3, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 12, '1', '2013-03-25 18:59:33'), (4, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 9, '0', '2013-03-25 19:00:41'), (5, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 5, '0', '2013-03-25 19:01:37'), (6, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 6, '0', '2013-03-25 19:01:43'), (7, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 1, '1', '2013-03-25 19:01:48'), (8, 'AXDUYNBAUIGC268', 'o9u8tno9qvrj8cfja2g1c516f5', 1, 9, '0', '2013-03-25 19:01:49'), (9, 'ZKHBILGVNJBB040', '8h8r13pnad0ftitsdjt9co49p0', 1, 14, '1', '2013-03-25 19:01:54'), (10, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 15, '1', '2013-03-25 19:02:08'), (11, 'AXDUYNBAUIGC268', 'o9u8tno9qvrj8cfja2g1c516f5', 1, 7, '0', '2013-03-25 19:02:09'), (12, 'ZKHBILGVNJBB040', '8h8r13pnad0ftitsdjt9co49p0', 1, 6, '1', '2013-03-25 19:02:10'), (13, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 14, '0', '2013-03-25 19:02:15'), (14, 'AXDUYNBAUIGC268', 'o9u8tno9qvrj8cfja2g1c516f5', 1, 6, '0', '2013-03-25 19:02:18'), (15, 'ZKHBILGVNJBB040', '8h8r13pnad0ftitsdjt9co49p0', 1, 13, '1', '2013-03-25 19:02:20'), (16, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 6, '0', '2013-03-25 19:50:30'), (17, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 9, '0', '2013-03-25 19:50:37'), (18, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 1, '1', '2013-03-25 19:50:45'), (19, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 4, '0', '2013-03-25 19:50:51'), (20, 'JOQHOIGNUFEC945', 'bb57a1nfodomuf0i01pcojs0f2', 1, 16, '0', '2013-03-25 19:52:58'), (21, 'JOQHOIGNUFEC945', 'bb57a1nfodomuf0i01pcojs0f2', 1, 13, '0', '2013-03-25 19:53:01'), (22, 'JOQHOIGNUFEC945', 'bb57a1nfodomuf0i01pcojs0f2', 1, 1, '0', '2013-03-25 19:53:08'); ------------------------------- Q:Yester day Query good but i need without ending "Over" , "Done" anonomous user like "sessionid " uj24ookkdcr3ris0cpbf82dd14 his user have 1 attempt and eqs39lv506p150esr274e9a810 user have 1 one attempt, any help please...? CREATE TABLE IF NOT EXISTS `maf_game` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(250) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; INSERT INTO `maf_game` (`id`, `name`) VALUES (1, 'Ag Across America'); CREATE TABLE IF NOT EXISTS `maf_game_attempts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userid` varchar(100) NOT NULL, `sessionid` varchar(100) NOT NULL, `gid` int(11) NOT NULL, `status` varchar(50) NOT NULL, `attempttime` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=31 ; INSERT INTO `maf_game_attempts` (`id`, `userid`, `sessionid`, `gid`, `status`, `attempttime`) VALUES (1, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'start', '2013-03-25 18:58:14'), (2, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'start', '2013-03-25 18:59:02'), (3, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'start', '2013-03-25 18:59:33'), (4, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'Over', '2013-03-25 19:00:41'), (5, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 'start', '2013-03-25 19:01:38'), (6, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 'start', '2013-03-25 19:01:44'), (7, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 'start', '2013-03-25 19:01:48'), (8, 'AXDUYNBAUIGC268', 'o9u8tno9qvrj8cfja2g1c516f5', 1, 'start', '2013-03-25 19:01:49'), (9, 'ZKHBILGVNJBB040', '8h8r13pnad0ftitsdjt9co49p0', 1, 'start', '2013-03-25 19:01:54'), (10, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 'start', '2013-03-25 19:02:08'), (11, 'AXDUYNBAUIGC268', 'o9u8tno9qvrj8cfja2g1c516f5', 1, 'start', '2013-03-25 19:02:09'), (12, 'ZKHBILGVNJBB040', '8h8r13pnad0ftitsdjt9co49p0', 1, 'start', '2013-03-25 19:02:10'), (13, 'PEHICJWEBEGA391', 'tpv2ebuhtnv5a8thfmltarsl65', 1, 'Over', '2013-03-25 19:02:15'), (14, 'AXDUYNBAUIGC268', 'o9u8tno9qvrj8cfja2g1c516f5', 1, 'Over', '2013-03-25 19:02:18'), (15, 'ZKHBILGVNJBB040', '8h8r13pnad0ftitsdjt9co49p0', 1, 'Done', '2013-03-25 19:02:20'), (16, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'start', '2013-03-25 19:50:30'), (17, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'start', '2013-03-25 19:50:37'), (18, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'start', '2013-03-25 19:50:45'), (19, 'JOQHOIGNUFEC945', 'hcn27d3i1kemmgubvti8ic3p87', 1, 'Over', '2013-03-25 19:50:51'), (20, 'JOQHOIGNUFEC945', 'bb57a1nfodomuf0i01pcojs0f2', 1, 'start', '2013-03-25 19:52:58'), (21, 'JOQHOIGNUFEC945', 'bb57a1nfodomuf0i01pcojs0f2', 1, 'start', '2013-03-25 19:53:01'), (22, 'JOQHOIGNUFEC945', 'bb57a1nfodomuf0i01pcojs0f2', 1, 'Over', '2013-03-25 19:53:08'), (23, 'GXJVKQTQIJMU436', 'uj24ookkdcr3ris0cpbf82dd14', 1, 'start', '2013-03-26 10:02:02'), (24, 'PEHICJWEBEGA391', 'eqs39lv506p150esr274e9a810', 1, 'start', '2013-03-26 10:05:06'), (25, 'PEHICJWEBEGA391', 'eqs39lv506p150esr274e9a810', 1, 'start', '2013-03-26 10:05:12'), (26, 'EHHYWVGQZKLA633', 'cptl7mvvb88r764n0654avc1l5', 1, 'start', '2013-03-26 10:17:40'), (27, 'EHHYWVGQZKLA633', 'cptl7mvvb88r764n0654avc1l5', 1, 'start', '2013-03-26 10:17:47'), (28, 'EHHYWVGQZKLA633', 'cptl7mvvb88r764n0654avc1l5', 1, 'start', '2013-03-26 10:17:56'), (29, 'EHHYWVGQZKLA633', 'cptl7mvvb88r764n0654avc1l5', 1, 'Over', '2013-03-26 10:18:05'), (30, 'EHHYWVGQZKLA633', 'cptl7mvvb88r764n0654avc1l5', 1, 'start', '2013-03-26 10:18:17');
答案 0 :(得分:2)
简而言之,这是如何做到的:
Select maf_game.name as daname , count(maf_game_attempts.id) as danum from maf_game,maf_game_attempts where maf_game_attempts.gid=maf_game.id and maf_game_attempts.status='over';
您可以放弃状态或将其更改为您需要的内容我认为超过播放意义。