0

我有这个查询,它与用户注册的课程的作业 ID 相呼应。

$sql = $db->prepare("SELECT assignments.*, enrollments.course_id, enrollments.student_id
                        FROM assignments
                        LEFT JOIN enrollments
                        ON assignments.course_id = enrollments.course_id
                        LEFT JOIN completed
                        ON assignments.id != completed.assignment_id
                        WHERE enrollments.student_id = ?
                        ORDER BY assignments.id DESC LIMIT 10
                        ");

$sql->execute(array($login_id));

while($row = $sql->fetch())
        {
                echo $row['id'];
        }

在我查看作业是否已标记为已完成的情况下进行另一次检查的最佳方法是什么?

这意味着它还需要检查“已完成”表,并确保对于任何选定的分配,没有 $login_id 和 assignment.id 一起出现的行。

这是我现在要查找已登录用户的已完成分配 ID 的查询。

$sqlcomplete = $db->prepare("SELECT * FROM completed
INNER JOIN students ON completed.student_id = students.id
WHERE completed.student_id = ?
");

$sqlcomplete->execute(array($login_id));

while($row = $sqlcomplete->fetch(PDO::FETCH_ASSOC))
{
echo "<li>You have completed assignment with ID ".$row['assignment_id']."</li>";
}

我试图做一个更复杂的JOIN,但我似乎无法弄清楚。我还考虑过简单地创建一个用户通过单独查询该数据库完成的分配的 ID 数组,并将该 ID 放入 while 检查中,但我觉得这不是最好或最有效的解决方案。

4

1 回答 1

2

您可以使用LEFT JOINand when completed.assignment_id IS NULLthen 这意味着已完成的表没有返回匹配项。

SELECT assignments.*, enrollments.course_id, enrollments.student_id
FROM assignments
 LEFT JOIN enrollments ON assignments.course_id = enrollments.course_id
 LEFT JOIN completed ON assignments.id = completed.assignment_id
WHERE enrollments.student_id = ?
AND completed.assignment_id IS NULL
ORDER BY assignments.id DESC LIMIT 10
于 2013-02-22T19:06:08.683 回答