简体   繁体   English

多对多关系sql

[英]many to many relationship sql

在此处输入图片说明

What can i add to these three tables to allow a "family" to have many students and a family to have many parents? 我可以在这三个表中添加些什么,以使一个“家庭”有很多学生,一个家庭有很多父母?

i am struggling to get my head around what i could add which would allow me to do this. 我正在努力使我能加的东西变得更容易使我做到这一点。

Thanks 谢谢

You need to add a foreign key in student and parent that maps back to family_id, Remove parent_id and student_id in the Family table. 您需要在学生和家长中添加一个外键,以映射回family_id,在Family表中删除parent_id和student_id。 If you need to find all member of family 22 如果您需要找到家庭的所有成员22

select * from family f 
join parent p on f.family_id=p.family_id
join student s on s.family_id=f.family_id
where f.family_id=22

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

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