内连接两个外键到一个主键

时间:2021-02-12 11:30:59

I have a table called branch (branchid, branchname) and another table called transfer transfer(tranferid, sourcebranch, destinationbranch)

我有一个名为branch的表(branchid, branchname)和另一个名为transfer transfer的表(tranferid, sourcebranch, destinationbranch)

both sourcebranch and destinationbranch are Fk to the branchid of of branch table. I need to show a query that looks like this

源分支和目标分支都是分支表的分支。我需要显示一个像这样的查询

Tranferid Source Destination 4 uk us

Tranferid来源目的地4 uk us

but all I can get is something like this

但我能得到的就是这样的东西

Tranferid Source Destinationid 4 uk 3

Tranferid来源Destinationid 4 uk 3。

query sample

查询示例

select tranferid, branch.branchname, transfer.destinationbranch from transfer inner join branch on branch.branchid == transfer.sourcebranch

选择tranferid,分支。分支机构:分支机构:分支机构。branchid = = transfer.sourcebranch

How do I get the destination branch to show. CTE on my mind

我怎样才能让目标部门显示出来。CTE在我心中

1 个解决方案

#1


5  

You need to join table branch on table transfer twice so you can get the value for each column.

您需要在表传输上连接表分支两次,以便为每个列获取值。

SELECT  a.*, 
        b.branchName AS sourceBranchName,
        c.branchName AS destinationBranchName
FROM    transfer a
        INNER JOIN branch b
            ON a.sourcebranch = b.branchID
        INNER JOIN branch c
            ON a.destinationbranch = c.branchID

To further gain more knowledge about joins, kindly visit the link below:

为了进一步了解加入的更多信息,请访问下面的链接:

#1


5  

You need to join table branch on table transfer twice so you can get the value for each column.

您需要在表传输上连接表分支两次,以便为每个列获取值。

SELECT  a.*, 
        b.branchName AS sourceBranchName,
        c.branchName AS destinationBranchName
FROM    transfer a
        INNER JOIN branch b
            ON a.sourcebranch = b.branchID
        INNER JOIN branch c
            ON a.destinationbranch = c.branchID

To further gain more knowledge about joins, kindly visit the link below:

为了进一步了解加入的更多信息,请访问下面的链接: