内部将一张桌子两次连接到另一张桌子-sqlite

时间:2019-09-27 12:29:28

标签: sqlite

感谢您的帮助。我有两个表,我想将一个表与另一个表进行内部联接两次。

CREATE TABLE "indexlist" (
    "id"    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    "word"  UNIQUE
)

INSERT INTO "indexlist" (words) VALUES
    ( 'run' ),
    ( 'hand' ),
    ( 'man' ),
    ( 'far' )

CREATE TABLE "nodes" (
    "source"    TEXT,
    "Target"    TEXT
)


INSERT INTO nodes ( source, target ) VALUES
    ( "run", "far" ),
    ( "far", "hand" );

我已经尝试过了。它不会抛出错误消息,但是不会显示任何结果:

SELECT t.id, t.word, 
   t1.target, t2.source
FROM indexlist t
LEFT JOIN nodes t1 ON t1.target=t.word
LEFT JOIN nodes t2 ON t.word=t2.source

我想输出下表:

source_id | source | target | target_id
=======================================
1         | run    | far    |         4

4         | far    | hand   |         2

1 个答案:

答案 0 :(得分:0)

您必须使用LEFT的{​​{1}}连接到nodes的2个副本进行连接:

indexlist

结果:

SELECT i1.id source_id, n.source, n.target, i2.id target_id
FROM nodes n
LEFT JOIN indexlist i1 ON n.source = i1.word
LEFT JOIN indexlist i2 ON n.target = i2.word