2016-04-03 59 views
0

我有一个数据库设计,如:查询了许多一对多的关系

t_relationships

  • ID(唯一的)
  • relationship_uuid
  • actor_uuid

t_actor

  • 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]

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

回答

0

您不需要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' 
+0

非常感谢! – Michal