[英]MySQL query to show a format based on three tables
我在下面的三個表格中顯示了學生記錄,學科和有學科的學生。
我想問什么是有效的SQL查詢,以顯示以下結果。 我可以使用JOIN來顯示它,但不能使用以下格式。
+------+-----------+-----------+-----+----------+-----------+--------+ | Name | Address | Telephone | Sex | Subjects | Teacher | Active | +------+-----------+-----------+-----+----------+-----------+--------+ | John | somewhere | 12345 | M | | Teacher 1 | YES | | John | somewhere | 12345 | M | Math | | YES | | John | somewhere | 12345 | M | Science | | YES | | John | somewhere | 12345 | M | English | | YES | | Matt | somewhere | 123456 | M | | Teacher 2 | YES | | Matt | somewhere | 23456 | M | Math | | YES | | Matt | somewhere | 123456 | M | Science | | YES | | Girl | somewhere | 5431 | F | | Teacher3 | YES | | Girl | somewhere | 5431 | F | Physics | | YES | | Girl | somewhere | 5431 | F | Math | | YES | +------+-----------+-----------+-----+----------+-----------+--------+ select * from student_record; +------------+------+-----------------+-----------+-----+----------+--------+ | id_student | name | address | telephone | sex | teacher | active | +------------+------+-----------------+-----------+-----+----------+--------+ | 1 | John | Somewhere | 12345 | M | Teacher | 0 | | 2 | Matt | Somewhere There | 12345222 | M | Teacher1 | 0 | | 3 | Girl | Somewhere here | 3333 | F | Teacher2 | 0 | +------------+------+-----------------+-----------+-----+----------+--------+ select * from subjects; +------------+--------------+---------------------+ | id_subject | subject_name | subject_description | +------------+--------------+---------------------+ | 1 | Math | Math | | 2 | Science | Science | | 3 | English | English | | 4 | Physics | Physics | +------------+--------------+---------------------+ select * from with_subjects; +--------------------+--------------------+------------+ | id_student_subject | student_id_subject | student_id | +--------------------+--------------------+------------+ | 1 | 1 | 1 | | 2 | 2 | 1 | | 3 | 3 | 1 | | 4 | 4 | 1 | | 5 | 4 | 2 | | 6 | 3 | 2 | | 8 | 1 | 2 | | 9 | 1 | 3 | | 10 | 2 | 3 | | 11 | 3 | 3 | | 12 | 4 | 3 | +--------------------+--------------------+------------+
怎么樣
選擇一個名稱作為“名稱”,一個地址作為“地址”,一個電話作為“電話”,一個性別作為“性別”,空作為“主題”,一個教師作為“教師”,一個活動在student_record中作為“活動”,作為聯合a。名稱作為“名稱”,a。地址作為“地址”,a。電話作為“電話”,a.sex作為“性別”,b.subject_name作為“主題”,空作為“教師”,a。活動為“活動”,來自(student_record作為內部聯接,with_subjects在a.id_student = c.student_id上為c)內部聯接主題為b在c.student_id_subject = b.id_subject
沒有測試過。 它的順序與您的示例相同,但是應該在那里包含所有數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.