如何在SQL查询中“双重”加入?

时间:2012-05-09 18:46:30

标签: sql sqlite

我有一个像这样的SQLite查询:

SELECT Books.title, People.name FROM Books, People WHERE People.ID=Books.AuthorID

其中Books.AuthorID是与人员表中的ID对应的ID号。

输出:

Title   Author
Book1   John Doe
Book2   Martha James

但是我想添加第3个字段(Books.EditorID),它也是一个必须在People中查找的ID,但是它可能是一个不同于{{的ID / Person。 1}}是。

期望的输出:

Books.AuthorID

如何实现这一目标?我相信某种JOIN查询?

2 个答案:

答案 0 :(得分:1)

SELECT b.title, a.name as author_name, e.name as editor_name
FROM Books b
INNER JOIN People a ON a.ID=b.AuthorID
INNER JOIN People e on e.ID=b.EditorID

答案 1 :(得分:0)

只需使用别名加入相同的表格即可... 示例即将推出。

SELECT 
    Book.Title,
        People.Name,
        People2.name
    FROM
        Books
    INNER JOIN
        People
    ON
        People.ID = Books.AuthorID
    INNER JOIN
        People People2
    ON
        People2.ID = People.ID