我如何从一对多的关系中选择所有的东西,在一对多的关系中选择几层?我不知道怎么把那些左连接都嵌套起来。
例子
There are many Institutions
Each Institution has many Departments
Each Department has many Forums
Each Forum has many Users
Each User has many Posts
Each Post has many Comments
查找机构的所有评论。
我到处搜索,但我不确定这是什么名字-链式嵌套一对多关系查询?我发现最接近的是SQL left join vs multiple tables on FROM line?
最佳答案
在不了解你的模式和做一些假设的情况下:
SELECT c.*
FROM
Institution i
INNER JOIN Department d ON d.InstitutionID = i.InstitutionID
INNER JOIN Forums f ON f.DepartmentID = d.DepartmentID
INNER JOIN `User` u ON u.ForumID = f.ForumID
INNER JOIN Post p ON p.UserID = u.UserID
INNER JOIN Comment c ON c.PostID = p.PostID
WHERE
i.InstitutionID = 42
关于mysql - 通过多对一关系的多层查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19055788/