我正在为即将到来的世界杯做预测游戏。
这些是我的表格:
CREATE TABLE `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`last_name` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `matches` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`venue` varchar(100) COLLATE utf8_bin NOT NULL,
`stage` varchar(100) COLLATE utf8_bin NOT NULL,
`teamA` varchar(3) COLLATE utf8_bin NOT NULL DEFAULT '',
`teamB` varchar(3) COLLATE utf8_bin NOT NULL DEFAULT '',
`goalsAinit` int(11) DEFAULT NULL,
`goalsBinit` int(11) DEFAULT NULL,
`goalsAadded` int(11) DEFAULT NULL,
`goalsBadded` int(11) DEFAULT NULL,
`penaltiesA` int(11) DEFAULT NULL,
`penaltiesB` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `predictions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_user` int(11) unsigned DEFAULT NULL,
`id_match` int(11) unsigned DEFAULT NULL,
`goalsAinit` int(11) DEFAULT NULL,
`goalsBinit` int(11) DEFAULT NULL,
`goalsAadded` int(11) DEFAULT NULL,
`goalsBadded` int(11) DEFAULT NULL,
`penaltiesA` int(11) DEFAULT NULL,
`penaltiesB` int(11) DEFAULT NULL,
`points` int(11) DEFAULT '60',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
我已经有一个系统可以在用户登录系统时获取积分:
for (var j in predictions) {
if (matches[i].id == predictions[j].id) {
if(predictions[j].score[0] != null && predictions[j].score[1]!= null){
if((matches[i].score[0] != null && matches[i].score[1]!= null) && (
(predictions[j].score[0] > predictions[j].score[1] && matches[i].score[0] > matches[i].score[1]) ||
(predictions[j].score[0] < predictions[j].score[1] && matches[i].score[0] < matches[i].score[1]) ||
(predictions[j].score[0] == predictions[j].score[1] && matches[i].score[0] == matches[i].score[1])
)){
//prediction correct, add points
}else{
//prediction incorrect
}
}
}
break;
}
得分是GoalAinit和goalsBinit
的数组但我怎样才能为所有用户排名?它应该经常更新(虽然它不一定是实时的)但我觉得好像我每次想要更新排名时为每个用户执行2号,DB会爆炸......
有什么建议吗?感谢
答案 0 :(得分:0)
我想出了这个QUERY来直接在mysql中计算预测的等级:
SELECT rank, id_user, first_name, last_name, iso, correct, points, total_predictions
FROM (
SELECT @rank:=@rank+1 AS rank, id_user, first_name, last_name, iso, correct, points, total_predictions
FROM (
SELECT p.id_user, u.first_name, u.last_name, iso,
SUM(IF((p.goalsAinit > p.goalsBinit AND m.goalsAinit > m.goalsBinit)
OR (p.goalsAinit < p.goalsBinit AND m.goalsAinit < m.goalsBinit)
OR (p.goalsAinit = p.goalsBinit AND m.goalsAinit = m.goalsBinit), 1, 0)) AS correct,
SUM(IF((p.goalsAinit > p.goalsBinit AND m.goalsAinit > m.goalsBinit)
OR (p.goalsAinit < p.goalsBinit AND m.goalsAinit < m.goalsBinit)
OR (p.goalsAinit = p.goalsBinit AND m.goalsAinit = m.goalsBinit), p.points, 0)) AS points,
COUNT(*) AS total_predictions
FROM predictions p
INNER JOIN matches_debug m ON m.id = p.id_match
LEFT JOIN users u ON p.id_user = u.id
LEFT JOIN flags f ON u.country = f.country
GROUP BY p.id_user
ORDER BY points DESC, correct DESC, total_predictions DESC, id_user
) AS rankings, (SELECT @rank:=0) AS r
) AS overall_rankings;
使用大型用户群执行是否安全?