繁体   English   中英

查询多对多关系

[英]Query for many-to-many relationship

我有一个设计如下的数据库:

t_relationships

  • id(唯一)
  • Relationship_uuid
  • actor_uuid

t_演员

  • id(唯一)
  • actor_uuid
  • name_or_whatever_doesnt_matter

关系表有许多具有一个relationship_uuid参与者。

我在有效查询方面遇到了问题,该查询将使我与给定演员有关系的所有演员。

例如,如果actor table具有条目[1,1,cat], [2,2,dog], [3,3,tree], [4,4,box]

relationship具有[1,1,1], [2,1,2], [3,1,3], [4,2,1] [5,2,4], [6,3,2], [7,3,4]

找出与猫有关系的最佳方法是什么?

您不需要id列。 您可以放下它们。 联接是您想要的

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