Php SQL高分
好的,我有两个MYSQL表:Php SQL高分,php,sql,mysql,aggregate-functions,Php,Sql,Mysql,Aggregate Functions,好的,我有两个MYSQL表: CREATE TABLE `sessions` ( `id` int(11) NOT NULL AUTO_INCREMENT, `userID` int(11) NOT NULL, `runTime` int(11) NOT NULL, `monstersKilled` int(11) NOT NULL, `profit` int(11) NOT NULL, `tasks` int(11) NOT NULL, `xpGain` int(1
CREATE TABLE `sessions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userID` int(11) NOT NULL,
`runTime` int(11) NOT NULL,
`monstersKilled` int(11) NOT NULL,
`profit` int(11) NOT NULL,
`tasks` int(11) NOT NULL,
`xpGain` int(11) NOT NULL,
`lastupdate` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
以及
正如您所看到的,sessions有一个指向用户ID的链接。现在我想创建一个总分很高的PHP文件,但由于我对PHP和MYSQL的了解很少,我不知道如何开始。总高分将按总运行时间排序
例如:
在用户中,我有用户1(cody)和用户2(Joe)。现在,在会话中,我有3个会话:
id, userID, runTime, monstersKilled, profit, tasks, xpGain, lastupdate
12, 1, 27, 14, 6200, 0, 5050, 1282325410
19, 1, 18, 1, 277, 1, 168, 1278897756
1968, 2, 195, 433, 111345, 4, 73606, 1280993244
打印输出应符合以下要求:
Place, username, Total Run Time, Total Monsters Killed, Total profit, Total tasks, Total Exp Gain
1. Joe, 195, 433,11345,4,73606
2. Cody, 55, 15, 1, 5218
使用:
这个脚本应该在一个整洁的表格中为您输出高分。感谢用于SQL查询的OMG小马
<?php
$dbhost = 'localhost'; // Database Host
$dbuser = 'user'; // Database User
$dbpass = 'password'; // Database Password
$dbname = 'database'; // Database Name
$db = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $db);
$limit = 10; // The number of users to show in the top highscores. Set to 0 to show all
$sql = 'SELECT u.user,
SUM(s.runtime) AS total_run_time,
SUM(s.monsterskilled) AS total_monsters_killed,
SUM(s.profit) AS total_profit,
SUM(s.tasks) AS total_tasks,
SUM(s.xpgain) AS total_xp_gained
FROM USERS u
JOIN SESSION s ON s.userid = u.userid
GROUP BY u.user
ORDER BY total_run_time DESC';
if ($limit > 0) {
$sql .= ' LIMIT '.$limit;
}
$get_scores = mysql_query($sql);
$scores = array();
$rank = 0;
while($score = mysql_fetch_array($get_scores)) {
++$rank;
$scores[] = '<td>'.$rank.'</td><td>'.$score['user'].'</td><td>'.$score['total_run_time'].'</td><td>'.$score['total_monsters_killed'].'</td><td>'.$score['total_profit'].'</td><td>'.$score['total_tasks'].'</td><td>'.$score['total_xp_gained'].'</td>';
}
echo '<table><tbody>';
echo '<tr><td>Place</td><td>Username</td><td>Total Run Time</td><td>Total Monsters Killed, Total profit</td><td>Total tasks</td><td>Total Exp Gain</td></tr><tr>';
echo implode('</tr><tr>', $scores);
echo '</tr></tbody></table>';
?>
回答得很好,我不太使用MySQL,所以不知道它是否支持行范围,但您可能希望有一个SELECT TOP n或SELECT range,因为对于许多用户,此查询很容易变得非常慢。@Tom Gullen:MySQL的LIMIT
语法相当于SQL Server的TOP
,实际上,比你考虑<代码>偏移< /代码>参数时更好。谢谢简化我的答案:
SELECT u.user,
SUM(s.runtime) AS total_run_time,
SUM(s.monsterskilled) AS total_monsters_killed,
SUM(s.profit) AS total_profit,
SUM(s.tasks) AS total_tasks,
SUM(s.xpgain) AS total_xp_gained
FROM USERS u
JOIN SESSION s ON s.userid = u.userid
GROUP BY u.user
ORDER BY total_run_time DESC
<?php
$dbhost = 'localhost'; // Database Host
$dbuser = 'user'; // Database User
$dbpass = 'password'; // Database Password
$dbname = 'database'; // Database Name
$db = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $db);
$limit = 10; // The number of users to show in the top highscores. Set to 0 to show all
$sql = 'SELECT u.user,
SUM(s.runtime) AS total_run_time,
SUM(s.monsterskilled) AS total_monsters_killed,
SUM(s.profit) AS total_profit,
SUM(s.tasks) AS total_tasks,
SUM(s.xpgain) AS total_xp_gained
FROM USERS u
JOIN SESSION s ON s.userid = u.userid
GROUP BY u.user
ORDER BY total_run_time DESC';
if ($limit > 0) {
$sql .= ' LIMIT '.$limit;
}
$get_scores = mysql_query($sql);
$scores = array();
$rank = 0;
while($score = mysql_fetch_array($get_scores)) {
++$rank;
$scores[] = '<td>'.$rank.'</td><td>'.$score['user'].'</td><td>'.$score['total_run_time'].'</td><td>'.$score['total_monsters_killed'].'</td><td>'.$score['total_profit'].'</td><td>'.$score['total_tasks'].'</td><td>'.$score['total_xp_gained'].'</td>';
}
echo '<table><tbody>';
echo '<tr><td>Place</td><td>Username</td><td>Total Run Time</td><td>Total Monsters Killed, Total profit</td><td>Total tasks</td><td>Total Exp Gain</td></tr><tr>';
echo implode('</tr><tr>', $scores);
echo '</tr></tbody></table>';
?>