表中的多个外键引用相同的主键另一个表 [英] Multiple foreign keys in a table referencing the same primary key another table

查看:272
本文介绍了表中的多个外键引用相同的主键另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了表中多个外键的要求

引用相同的主键另一个表。

表结构如下:

表名

i came across requirement of multiple foreign keys in a table
referencing the same primary key another table.
Table structure is as follows:
Table Name

引用:

File_Master

File_Master



file_id

file_no

from_Designation_id

to_Designation_id



表名


file_id
file_no
from_Designation_id
to_Designation_id

Table Name

引用:

Designation_Master

Designation_Master



Designation_id

Designation_name



File_Master有多个外国人密钥(from_Designation_id,to_Designation_id),参考Designation_Master Designation_id(pk)。



现在任何人都可以帮我加入以上两个表格如下

file_id,file_no,from_Designation,to_Designation


Designation_id
Designation_name

File_Master has multiple foreign keys(from_Designation_id,to_Designation_id) which reference Designation_Master Designation_id(pk) .

Now can anyone please help me to join these above two table as follows
file_id, file_no, from_Designation, to_Designation

推荐答案

尝试



Try

SELECT file_id, file_no, DM1.Designation_name AS from_Designation, DM2.Designation_name AS to_Designation
FROM File_Master F
LEFT JOIN Designation_Master DM1 ON F.from_Designation_id = DM1.Designation_id
LEFT JOIN Designation_Master DM2 ON F.to_Designation_id = DM2.Designation_id


这篇关于表中的多个外键引用相同的主键另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆