0

我有一个这样的 SQLite 查询:

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

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

输出:

Title   Author
Book1   John Doe
Book2   Martha James

但是我想添加第三个字段(Books.EditorID),它也是一个必须在其中查找的 ID People,但很可能它会是一个不同的 ID/Person Books.AuthorID

期望的输出:

Title   Author        Editor
Book1   John Doe      Jack Brown
Book2   Martha James  Bob Nelson

如何实现?我相信某种 JOIN 查询?

4

2 回答 2

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
于 2012-05-09T18:49:35.150 回答
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
于 2012-05-09T18:47:57.630 回答