简体   繁体   English

如何根据第三个表中的关系合并两个表?

[英]How to merge two tables according to the relation in a third table?

Suppose there are 3 tables: 假设有3个表:

student 学生

teacher 老师

advisor 顾问

Student has name and id columns, teacher also has name and id columns and advior has s_id (stands for student id) and t_id (stands for teacher id). 学生有姓名和id列,老师也有姓名和id列,advior有s_id (代表学生ID)和t_id (代表老师id)。 s_id references to id in student table and t_id references to id in teacher table. s_id引用student表中的id,t_id引用teacher表中的id。 How can I merge the name of the student with name of his advisor? 如何将学生的姓名与其顾问的名字合并? Thanks for any help. 谢谢你的帮助。

只需在学生表中创建主管ID,即可将主管与学生合并,并使用join Statement

You can use join to merge the tables. 您可以使用join来合并表。

Try this: 尝试这个:

select b.name,c.name from advisor a
join student b on a.s_id = b.id
join teacher c on a.t_id = c.id
where a.s_id = 1

Note that a, b and c are aliases. 请注意,a,b和c是别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM