一个 table 有 2 个外键引用同一个 table

A table with 2 foreign keys referencing the same table

我有一个 table,其中有 2 个外键引用同一个 table。想怎样把加入的内容打印出来?我正在使用 SQLite

表A

S.NO  Col1   Col2
1    | a   |  b
2    | f   |  g
3    | d   |  e

表B

S.NO  Col3 
a    | apple   
b    | ball
f    | frog
g    | grape
d    | dog
e    | eat

如何打印出以下内容?

S.NO |Col1  | Col2 | Col3| Col4
1    | a    |apple |  b  | ball
2    | f    |frog  |  g  | grape
3    | d    |dog   |  e  | eat

我试过了

SELECT TableA.Col1, TableB.Col3, TableA.Col1 
FROM TableA, TableB
WHERE TableA.Col1 = TableB.Col3
AND TableA.Col2 = TableB.Col3

感谢您的帮助。

两个内连接即可:

select 
    a.sno, a.col1, b1.col3, a.col2, b2.col3
  from tablea a
  join tableb b1 on b1.sno = a.col1
  join tableb b2 on b2.sno = a.col2

注意 tableb 加入了两次。在这种情况下,您需要每次都给它一个不同的别名:b1b2。否则将没有明确的方法来引用每个列。