到目前为止,我认为我做得对,但我似乎无法正确显示grade_points
这里是我在下面显示的内容。如何修复它以便我的代码正确显示grade_points
?
输出数据
这是我目前的输出:
user_id Array ( [1] => 1 [3] => 2 )
rank Array ( [0] => 1 [1] => 3 )
grade_points Array ( [0] => [1] => )
users Array ( [0] => 3 [1] => 2 )
the rank of user_id 3 is #2
以下是我想输出的内容:
user_id Array ( [1] => 1 [3] => 2 )
rank Array ( [0] => 1 [1] => 3 )
grade_points Array ( [0] => 8 [1] => 5 )
users Array ( [0] => 3 [1] => 2 )
the rank of user_id 3 is #2
PHP& MySQL代码
这是我的PHP& MySQL代码。
$i = 1;
$u = array();
$user = array();
$rank = array();
$gp = array();
$dbc = mysqli_query($mysqli,"SELECT SUM(grade_points) as p, grades.grade_points, assignment_grades.*, COUNT(*) as u, users_assignment.user_id
FROM users_assignment
LEFT JOIN grades ON users_assignment.user_id = grades.letter_grade
LEFT JOIN assignment_grades ON grades.id = assignment_grades.grade_id
GROUP BY users_assignment.user_id
ORDER BY p DESC");
if (!$dbc) {
print mysqli_error($mysqli);
} else {
while($row = mysqli_fetch_array($dbc)){
$u[$row['user_id']] = $i++;
$rank[] = $row['user_id'];
$user[] = $row['u'];
$gp[] = $row['p'];
}
}
echo 'user_id '; print_r($u); echo '<br />';
echo 'rank '; print_r($rank); echo '<br />';
echo 'points '; print_r($gp); echo '<br />';
echo 'users '; print_r($user);
echo "the rank of user_id 3 is" . $u[3];
MySQL表格
CREATE TABLE assignment_grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
grade_id INT UNSIGNED NOT NULL,
users_assignment_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_assignment (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
assignment_content LONGTEXT NOT NULL,
grade_average VARCHAR DEFAULT NULL,
PRIMARY KEY (id)
);
表数据
assignment_grades
id grade_id users_assignment_id
15 15 35
16 16 35
17 17 33
等级
id letter_grade grade_points
15 C+ 3
16 A+ 5
17 A+ 5
users_assignment
id user_id assignment_content grade_average
32 1 some content NULL
33 1 some content A+
34 3 some content NULL
35 3 some content B+
36 1 some content NULL
答案 0 :(得分:0)
我认为问题出在您的查询中。尝试将SUM(grade_points)替换为SUM(grade.grade_points)
答案 1 :(得分:0)
您的查询的联接未正确匹配字段 - 请尝试以下查询:
SELECT SUM(grade_points) as p, grades.grade_points, assignment_grades.*, COUNT(*) as u, users_assignment.user_id
FROM users_assignment
LEFT JOIN assignment_grades ON users_assignment.id = assignment_grades.users_assignment_id
LEFT JOIN grades ON grades.id = assignment_grades.grade_id
GROUP BY users_assignment.user_id
ORDER BY p DESC