mysql作业清单

时间:2018-07-17 16:12:42

标签: mysql

我有两个表: 表学生和学校作业表

Students table
+--------------------------+---------------------------------+
| id                       |   name                          |
+--------------------------+---------------------------------+
| 1                        |   ADAM                          |
| 2                        |   BRIGITTE                      |
| 3                        |   ANNE                          |
+--------------------------+---------------------------------+

table student works
+---------------+-------------------------+------------------+
| id_works      |   works                 |   id_student     |
+---------------+-------------------------+------------------+
| 1             |   airplane wing         |   1              |
| 2             |   volcano               |   2              |
| 3             |   law of gravity        |   1              |
| 4             |   airplane wing         |   3              |
| 5             |   law of gravity        |   1              |
+-----------------------------------------+------------------+

如何对返回整个学生列表的作业进行选择,以表明作业已交付? (重要:所有学生的名单)

示例

LIST FOR WORK **airplane wing**
+--------------------------+---------------------------------+
|  ADAM                    |   X                             |
|  BRIGITTE                |                                 |
|  ANNE                    |   X                             |
+--------------------------+--------------------- -----------+

我已经用LEF JOIN和IF进行过尝试,但这不是所有学生中没有重复的列表。

SELECT
    s.name ,
    w.work,
    w.resid_id,
    if(w.work  = 'airplane wing', 'X', '') as mark

    FROM  students s
    LEFT JOIN works w
    ON  s.id = w.id_student

    ORDER BY s.name ASC

1 个答案:

答案 0 :(得分:1)

这将为您列出所有学生 对于未完成工作的人,字段id_worksworks将为空

SELECT s.name, w.id_works, w.works

FROM  students s
LEFT JOIN works w
    ON (w.id_student = s.id AND w.works = 'airplane wing')

ORDER BY s.name ASC