[英]How do i get the ID from parent table and JOIN from child table to run the query.
我有兩個桌子,一個是學生,另一個是學校。 我正在使用FK將學校ID( scid
)調用到學生表。
學生們
+------+---------+-----+-----------+
| stid | student | age | school_id |
+------+---------+-----+-----------+
| 1 | John | 26 | 2 |
| 2 | Susan | 24 | 1 |
+------+---------+-----+-----------+
學校
+------+------------+-----------+
| scid | schoolname | syllabus |
+------+------------+-----------+
| 1 | school1 | syllabus1 |
| 2 | school2 | syllabus2 |
+------+------------+-----------+
視圖頁面顯示了所有詳細信息,其中我正在使用ID從學生表中獲取詳細信息,並使用聯接從學校表中獲取信息。
Student name: John
Age: 26
School: School 2
我需要為學生使用ID,並使用聯接從學校獲得名稱。 我怎么做。 我想出了這個,但是我該如何使用id。
SELECT schools.name
FROM schools
LEFT JOIN students
ON students.school_id=schools.scid;
您可以通過以下方式進行操作:
select student, age, schoolname
from students stdu inner join schools sch
on(stdu.school_id=sch.scid )
where stdu.stid = 1
MySQL INNER JOIN
子句將一個表中的行與其他表中的行進行匹配,並允許您查詢包含兩個表中的列的行。 https://dev.mysql.com/doc/refman/5.7/en/join.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.