如何对查询中的条目进行分类?

How to classify entries in a query?

我有三个表:student、assignment 和 subject。它们具有以下值:

学生

student_id student_name subject_id
2001 John MacEnroe 11
2002 May Rockwell 13
2005 James Cronwell 13
2012 Jane Crickett 14

主题

subject_id subject_name
11 Math
13 History
14 Biology

作业

student_id subject_id description score submission_date
2001 11 assignment 1 8.0 2021-05-13 20:15:20
2001 11 assignment 2 8.0 2021-06-15 21:35:40
2005 13 assignment 1 4.0 2021-05-13 19:05:30

我想创建一个查询,returns 向我提供所有学生和相关科目的列表,每个科目的状态遵循以下规则,以及最后一次作业评估的日期.

预期输出:

student_name subject_name description status submission_date
John MacEnroe Math assignment 1 Passed 2021-05-13 20:15:20
John MacEnroe Math assignment 2 Passed 2021-06-15 21:35:40
James Cronwell History assignment 1 Exam 2021-05-13 19:05:30
May Rockwell History Pending
Jane Crickett Biology Pending

我尝试使用下面的代码,但没有用。我卡住了。我很感激任何帮助。

SELECT 
 student_name
,subject_name
,submission_date
 from 
student
,subject
,assignments;

这就是您要查找的内容:

SELECT 
  s.`student_name`,
  sub.`subject_name`,
  IFNULL(a.`description`,'') description,
  (CASE WHEN a.`score` IS NULL THEN 'Pending'
        WHEN a.`score`>=5 THEN 'Passed'
        ELSE 'Exam' END) `status`,
  IFNULL(a.`submission_date` ,'') submission_date
FROM
  `student` s 
  LEFT JOIN `Subject` sub  ON s.`subject_id` = sub.`subject_id` 
  LEFT JOIN `Assignments` a  ON a.`student_id` = s.`student_id` 

从没有 table 关系的多个 table 中选择会给你 Cartesian product of all associated tables.