[英]Query for many-to-many relationship
I've got a database designed like: 我有一个设计如下的数据库:
t_relationships t_relationships
t_actor t_演员
The relationship table has many actors with one relationship_uuid
. 关系表有许多具有一个
relationship_uuid
参与者。
I'm having problems with efficient query that will give me all the actors in a relationship with a given actor. 我在有效查询方面遇到了问题,该查询将使我与给定演员有关系的所有演员。
For instance, if actor table
have entries [1,1,cat], [2,2,dog], [3,3,tree], [4,4,box]
例如,如果
actor table
具有条目[1,1,cat], [2,2,dog], [3,3,tree], [4,4,box]
and relationship
has [1,1,1], [2,1,2], [3,1,3], [4,2,1] [5,2,4], [6,3,2], [7,3,4]
. 和
relationship
具有[1,1,1], [2,1,2], [3,1,3], [4,2,1] [5,2,4], [6,3,2], [7,3,4]
What's the best way to find out who is in a relationship with a cat? 找出与猫有关系的最佳方法是什么?
You don't need the id
columns. 您不需要
id
列。 You can just drop them. 您可以放下它们。 A join is what you are looking for
联接是您想要的
select ar.name
from t_actor a
join t_relationships r on r.actor_uuid = a.actor_uuid
join t_actor ar on ar.actor_uuid = r.relationship_uuid
where a.name = 'cat'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.