使用ids匹配2个名称

时间:2018-03-13 10:16:29

标签: sql

我有2张桌子。

校长表(人名为id):

 ID     NAME
 14     Person1
 29     Person2

替换表(匹配2个具有主要ID的人):

SUB1ID   SUB2ID
 14       29

我想在替换表中选择名称,如:

SUB1ID   NAME1     SUB2ID   NAME2
14       Person1   29       Person2

2 个答案:

答案 0 :(得分:2)

SELECT s1.SUB1ID,p1.Name,s1.SUB2ID,p2.name FROM principal p1
INNER JOIN substitution s1
ON p1.id=s1.SUB1ID
INNER JOIN principal p2
ON p2.id=s1.SUB2ID

输出

SUB1ID  Name    SUB2ID  name
14      Person1 29      Person2

答案 1 :(得分:1)

SELECT
    T.Sub1ID,
    P1.Name,
    T.Sub2ID,
    P2.Name
FROM
    Substitutions T
    INNER JOIN Princial P1 ON T.Sub1ID = P1.ID
    INNER JOIN Princial P2 ON T.Sub2ID = P2.ID