我是symfony2开发的新手。我正在努力开发eschool课程管理系统,这是我对6个表的总结。
Students
----------
Id Firstname
1 John
2 Susan
3 Sarah
Courses
----------
Id Name
1 ICT
2 Math
3 PE
Students_Courses
----------
Student_Id Course_Id
1 1
1 3
2 2
2 1
3 1
Grades
----------
Id Student_Id Course_Id Grade Quarter_Id
1 1 1 90 1
2 2 1 80 1
Quarters
-----------
Id Name
1 Semester 1
2 Semester 2
3 Semester 3
4 Semester 4
Quarters_Courses
------------
Quarter_id Course_Id
1 1
3 1
2 2
4 3
我正在使用下面的查询
SELECT * FROM Students AS s LEFT JOIN Grades as g on g.student_id = s.id where g.quarter_id=1
它显示了我的结果
John 90
Susan 80
但是我想查询显示以下结果(如果quarter_id等于1)
John 90
Susan 80
Sarah
答案 0 :(得分:0)
将您的条件设置为 JOIN 而不是 WHERE 条件,因为如果您将右表的任何条件放在where条件中,那么它将被视为 INNER JOIN 而不是 LEFT JOIN:
试试这个:
SELECT s.Firstname, IFNULL(g.Grade, '') AS Grade
FROM Students AS s
LEFT JOIN Grades as g on g.student_id = s.id AND g.quarter_id=1;