简体   繁体   English

查询多对多关系

[英]Query for many-to-many relationship

I've got a database designed like: 我有一个设计如下的数据库:

t_relationships t_relationships

  • id (unique) id(唯一)
  • relationship_uuid Relationship_uuid
  • actor_uuid actor_uuid

t_actor t_演员

  • id (unique) id(唯一)
  • actor_uuid actor_uuid
  • name_or_whatever_doesnt_matter name_or_whatever_doesnt_matter

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.

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