gpt4 book ai didi

php - MYSQL和php问题

转载 作者:可可西里 更新时间:2023-11-01 07:54:07 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的脚本,该脚本将从数据库中获取数据并显示特定测验中得分最高的前 10 名最佳人选。这是 PHP 脚本:

<?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);
?>

但是我有一个问题。一个人可以多次参加测验。该脚本有效,但如果同一用户 (Data5) 的得分较高,则在前十名结果中显示两次,我只想显示一次在总体得分中得分最高的那个。在此图片中,您将看到我的数据库中有哪些数据,因此也许这可以帮助您更多地了解我的问题: enter image description here

最佳答案

更改查询:

正确答案 -

这是你需要的:

$result = mysql_query("SELECT 候选人, 资料5, 数据3, MAX(OverAllScore), MAX(总可能得分), MAX(PercentageScore), MAX(PercentagePass), 最大(通过失败)来自 {$table}WHERE QuizName='测试测验#505853118-1'按注册名、候选人、位置分组ORDER BY MAX(OverAllScore) DESC LIMIT 10");

关于php - MYSQL和php问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9035088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com