使用一个sqlite3查询从两个不同的行检索相同的列数据?

时间:2011-07-20 17:24:56

标签: python sqlite

我有一个表profiles,其中包含主键login_hash和一堆其他字段(例如age)的人。另一个表msgs包含srcdest字段,这两个字段都是login_hash表中profiles的外键。如何在一个sqlite3(来自Python)查询中检索src和dests的年龄?

1 个答案:

答案 0 :(得分:1)

您可以使用两个内部联接:

SELECT m.*, p1.*, p2.*
  FROM msgs m
INNER JOIN profiles p1 ON m.src = p1.login_hash
INNER JOIN profiles p2 ON m.dest = p2.login_hash