需要MYSQL查询才能找到每位玩家的最低分数

时间:2010-04-30 05:08:40

标签: mysql

我在Facebook上有一款名为Rails Across Europe的游戏。我有一个最佳得分页面,其中我向玩家展示了最好的20个分数,在游戏术语中指的是最低的获胜回合。问题是有少数玩家经常玩,他们的分数在页面中占主导地位。我想让分数页向更多玩家开放。所以我认为我可以为每个玩家显示单个最低的获胜回合,而不是显示所有玩家的所有最低获胜回合。问题是对此的查询躲过了我。所以我希望你们这位出色的StackOverflow人员可以帮助我。

我在下面列出了相关的MYSQL表模式。以下是表关系:

  • player_stats包含正在进行的游戏或已完成游戏的统计信息。如果游戏正在进行中,则wins_turn为零(这意味着wins_turn为零的游戏不应包含在查询中)。 player_stats有一个game_player表id引用。

  • game_player包含描述当前正在进行的游戏的数据。 game_player有一个玩家表id参考。

  • 播放器包含描述玩游戏的人的数据。

以下是我目前正在使用的查询:

  'SELECT p.fb_user_id, ps.winning_turn, gp.difficulty_level, c.name as city_name, g.name as goods_name, d.cost
   FROM game_player as gp, player as p, player_stats as ps, demand as d, city as c, goods as g
   WHERE p.status = "ACTIVE" AND gp.player_id = p.id AND ps.game_player_id = gp.id
   AND d.id = ps.highest_demand_id AND c.id = d.city_id AND g.id = d.goods_id
   AND ps.winning_turn > 0
   ORDER BY ps.winning_turn ASC, d.cost DESC LIMIT '.$limit.';';

以下是相关的表格模式:

--
-- Table structure for table `player_stats`
--

CREATE TABLE IF NOT EXISTS `player_stats` (
  `id` int(11) NOT NULL auto_increment,
  `game_player_id` int(11) NOT NULL,
  `winning_turn` int(11) NOT NULL,
  `highest_demand_id` int(11) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `game_player_id` (`game_player_id`,`highest_demand_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3814 ;

--
-- Table structure for table `game_player`
--

CREATE TABLE IF NOT EXISTS `game_player` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `game_id` int(10) unsigned NOT NULL,
  `player_id` int(10) unsigned NOT NULL,
  `player_number` int(11) NOT NULL,
  `funds` int(10) unsigned NOT NULL,
  `turn` int(10) unsigned NOT NULL,
  `difficulty_level` enum('STANDARD','ADVANCED','MASTER','ULTIMATE') NOT NULL,
  `date_last_used` datetime NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `game_id` (`game_id`,`player_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3814 ;

--
-- Table structure for table `player`
--

CREATE TABLE IF NOT EXISTS `player` (
  `id` int(11) NOT NULL auto_increment,
  `fb_user_id` char(255) NOT NULL,
  `fb_proxied_email` text NOT NULL,
  `first_name` char(255) NOT NULL,
  `last_name` char(255) NOT NULL,
  `birthdate` date NOT NULL,
  `date_registered` datetime NOT NULL,
  `date_last_logged_in` datetime NOT NULL,
  `status` enum('ACTIVE','SUSPENDED','CLOSED') NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `fb_user_id` (`fb_user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1646 ;

2 个答案:

答案 0 :(得分:0)

我没有时间真正深入研究你的桌子defs,但我在游戏中做了同样的事情,所以我只是给你我的疑问:

(我实际上获得了应该在高分榜中显示的所玩游戏的ID)

SELECT matchId
FROM (
    SELECT accountId,
           MIN(finalScore) as bestScore,
           scoringType
    FROM PlayedMatches
    GROUP BY accountId
) AS x INNER JOIN PlayedMatches AS f
ON f.accountId = x.accountId AND
   f.finalScore = x.bestScore
ORDER BY finalScore DESC
LIMIT 20

您可以在

上阅读有关此类查询的更多信息

http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

答案 1 :(得分:0)

DISTINCT运算符放在您选择的p.fb_user_id之前,看看会发生什么情况可能是值得的。这可能就是诀窍。 (免责声明:我从未以这种方式使用DISTINCT运算符,并且不确定它将起作用。)