我试图让一个学生的等级达到一百级,我希望能够显示学生的等级。
我试图按成绩和成绩获得学生的等级。例如,如果student 1
有2个A +等级,总共10个点而student 2
有3个B等级,总共10个等级student 1
将排名更高。我想知道如何使用PHP& amp; MySQL的?
这是PHP&我到目前为止的MySQL代码。
$gp = array();
$dbc = mysqli_query($mysqli,"SELECT grades.*, homework_grades.*, users_homework.*
FROM grades
LEFT JOIN homework_grades ON grades.id = homework_grades.grade_id
LEFT JOIN users_homework ON homework_grades.users_homework_id = users_homework.id
WHERE users_homework.user_id = '$user_id'
AND users_homework.grade_average = 'A+'");
if (!$dbc) {
print mysqli_error($mysqli);
} else {
while($row = mysqli_fetch_array($dbc)){
$gp[] = $row['grade_points'];
}
}
echo array_sum($gp);
这是我的MySQL表格。
CREATE TABLE homework_grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
grade_id INT UNSIGNED NOT NULL,
users_homework_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
letter_grade VARCHAR NOT NULL,
grade_points FLOAT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);
CREATE TABLE users_homework (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
homework_content LONGTEXT NOT NULL,
grade_average VARCHAR DEFAULT NULL,
PRIMARY KEY (id)
);
答案 0 :(得分:2)
更新
你需要一个聚合。执行SUM()和COUNT()。 COUNT将告诉您作业的数量,总和将告诉您总成绩点数。 GROUP BY user_id为每个学生提供此功能。
$gp = array();
$dbc = mysqli_query($mysqli,"SELECT SUM(grade_points) as grade_points, user_id, count(*) as num_assignments
FROM grades
LEFT JOIN homework_grades ON grades.id = homework_grades.grade_id
LEFT JOIN users_homework ON homework_grades.users_homework_id = users_homework.id
GROUP BY user_id
ORDER BY grade_points DESC , num_assignments ASC'");
if (!$dbc) {
print mysqli_error($mysqli);
} else {
$i=1;
while($row = mysqli_fetch_array($dbc)){
$user[$row['user_id']] = $i++;
$rank[] = $row['user_id'];
$gp[] = $row['grade_points'];
}
}
// the rank of user 10 is
echo "the rank of user_id 10 is {$user[10]}";
echo "the rank of all users are: " ;
print_r($rank);
这将累加成绩点,并计算作业数量。排序将确保从3个作业获得10分的学生将比从4个作业获得10分的学生具有更高的等级。
请参阅COUNT SUM和GROUP BY聚合。