Sqlite3:从多个表中选择,不重复

时间:2017-03-22 23:46:39

标签: join sqlite intersect

我有三张桌子:

paper:             items:               attachments:
============       ==============       ==============
jkey | title       itemID | jkey*       itemID* | path

*来自另一个表的外键

我试图检索所有论文的标题及其相关的附件路径,以及所有附件的论文。

目前的尝试是:

SELECT paper.title,attachments.path IN paper,attachments
WHERE paper.jkey IN (
    SELECT items.jkey FROM items,attachments
    WHERE items.itemID = attachments.itemID
);

不幸的是,这似乎打印出乱码(不同标题的路径相同,反之亦然)。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果你想加入,你应该使用连接:

SELECT paper.title,
       attachments.path
FROM paper
JOIN items USING (jkey)
JOIN attachments USING (itemID);

要省略重复的行,请改用SELECT DISTINCT ...