안녕하세요 저는 데이터베이스에서 가장 높은 점수를 가진 상위 10 개 결과를 얻는 간단한 스크립트를 만들었습니다. 여기 코드입니다 :PHP와 MySql 쿼리
<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = 'localhost';
$db_user = 'aa_quiz';
$db_pwd = 'mypassword';
$database = 'aa_quiz';
$table = 'aaresult';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT Candidate, Data5, Data3, OverAllScore, TotalPossibleScore, PercentageScore, PercentagePass, PassFail FROM {$table} WHERE QuizName='Test Quiz #505853118-1' ORDER BY OverAllScore DESC LIMIT 10");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach(..) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>
하지만 문제가 있습니다. 사람이 퀴즈를 한 번 더 가져갈 수 있으며, 그 사람이 OverAllScore에서 더 높은 점수를 가진 두 번을 만들면 상위 10 개에 두 번 표시됩니다. 이제 상위 10 개 결과에 내 스크립트에 어떻게 추가 할 수 있습니까? Data3 coloumn은 한 번 더 같은 값이며, 그 다음에 OverAllScore에서 가장 높은 수치를 가진 사람이 한 번만 표시됩니다.
이 수도로 선택 쿼리를 사용할 수 복용 사람을 당신을 위해 그것을 할 것입니다 ...
SELECT DISTINCT Candidate
시도 도움이 되십시오. http://dev.mysql.com/doc/refman/5.0/en/example-maximum-row.html – rd42