![](/img/trans.png)
[英]Composite primary key comprising two foreign keys referencing same table: SQL Server vs. MySQL
[英]How do I make a join on two tables in SQL when there are two foreign keys referencing the same primary key? It's driving me insane
标题说什么。 我尝试使用两个外键引用相同的主键连接两个表,但无法使其正常工作。
使用表别名。 例如:
select *
from money_transfer t
join account s on t.sender_id = s.id
join account r on t.receiver_id = r.id
在这里,您可以看到对同一个 PK ( account.id
) 的两个引用。 第一次为该表指定别名s
,第二次为同一表指定别名r
。 这样,您可以区分要在每个连接谓词上使用哪一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.