[英]MySQL Selecting all records from one table and their matches/NULL from another table
我有三張桌子。 其中兩個是獨立的無關表(學生和科目),第三個(條目)是將它們都與外鍵(student_id和subject_id)鏈接的表。
這是所有帶有記錄的表:
學生們:
+------------+------------+-----------+---------------------+---------------------+
| student_id | first_name | surname | email | reg_date |
+------------+------------+-----------+---------------------+---------------------+
| 1 | Emily | Jackson | emilym@gmail.com | 2012-10-14 11:14:13 |
| 2 | Daniel | ALexander | daniela@hotmail.com | 2014-08-19 08:08:23 |
| 3 | Sarah | Bell | sbell@gmail.com | 1998-07-04 13:16:32 |
| 4 | Alex | Harte | AHarte@hotmail.com | 1982-06-14 00:00:00 |
+------------+------------+-----------+---------------------+---------------------+
主題:
+------------+--------------+------------+----------------+
| subject_id | subject_name | exam_board | level_of_entry |
+------------+--------------+------------+----------------+
| 1 | Art | CCEA | AS |
| 2 | Biology | CCEA | A |
| 3 | Computing | OCR | GCSE |
| 4 | French | CCEA | GCSE |
| 5 | Maths | OCR | AS |
| 6 | Chemistry | CCEA | GCSE |
| 7 | Physics | OCR | AS |
| 8 | RS | CCEA | GCSE |
+------------+--------------+------------+----------------+
項:
+----------+---------------+---------------+------------+
| entry_id | student_id_fk | subject_id_fk | entry_date |
+----------+---------------+---------------+------------+
| 1 | 1 | 1 | 2012-10-15 |
| 2 | 1 | 4 | 2011-09-21 |
| 3 | 1 | 3 | 2015-08-10 |
| 4 | 2 | 6 | 1992-07-13 |
| 5 | 3 | 7 | 2013-02-12 |
| 6 | 3 | 8 | 2016-01-14 |
+----------+---------------+---------------+------------+
我該如何選擇所有學生的姓名(students.first_name)和他們擁有的任何學科條目的姓名(subjects.subject_name)? 我的意思是,將返回所有學生姓名,並在其旁邊列出其條目的所有主題名稱,對於沒有條目的學生,其名稱為NULL。 我也希望按名字將其按字母順序分組。
我希望輸出是這樣的:
first_name subject_name
--------------------------
Alex NULL
Daniel Chemistry
Emily French
Emily Computing
Sarah Physics
Sarah RS
盡管我不確定這是否完全正確。
我的猜測是這樣的:
SELECT students.first_name, subjects.subject_name
FROM students
JOIN entries ON entries.student_id_fk = students.student_id
JOIN subjects ON entries.subject_id_fk = subjects.subject_id
GROUP BY students.first_name;
實際上,我唯一不確定的是要使用哪個聯接以及將聯接放在何處,或者是否需要以不同順序列出表。 我認為這應該是左聯接,但不確定。
非常感謝幫助!
我認為這可能是查詢,但使用order by而不是group by(group by用於諸如count()或max()的聚合函數)
SELECT students.first_name, subjects.subject_name
FROM entries
LEFT JOIN students ON entries.student_id_fk = students.student_id
LEFT JOIN subjects ON entries.subject_id_fk = subjects.subject_id
ORDER BY students.first_name;
您應該使用左聯接,因為您還想為未分配主題的學生返回行。 另外,您想對結果進行排序,而不是對結果進行分組。 所以最后應該是這樣的:
SELECT students.first_name, subjects.subject_name
FROM students
LEFT JOIN entries ON entries.student_id_fk = students.student_id
LEFT JOIN subjects ON entries.subject_id_fk = subjects.subject_id
ORDER BY students.first_name;
您可能需要LEFT OUTER JOIN
:
select stu.first_name, sub.subject_name
from students stu
join entries e
on stu.student_id = e.student_id_fk
left outer join subjects sub
on sub.subject_id = e.subject_id_fk
order by stu.first_name;
我遠離安裝了任何SQL的PC,因此未經測試,但是可以滿足您的需求。
order by
替換該group by
。 這是執行此操作的正確方法,除非該組作為您可能已修剪掉的較大查詢的一部分存在。 在其余兩個表上按順序左聯接學生表是獲取所有學生名字的正確選擇。
要訂購名稱,應使用by by 。
我創建了一個sql小提琴,以更好地可視化如何獲得所需的輸出。
這里是鏈接: http : //sqlfiddle.com/#!9/a1c850/7
查詢是
select students.student_name, subjects.subject_name
from
students
left join entries on entries.student_id=students.student_id
left join subjects on entries.subject_id=subjects.subject_id
order by students.student_name;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.