You can use parentheses to prioritize joins. Like:
SELECT *
FROM people p
LEFT JOIN ( people_has_book pb JOIN books b USING (book_id) ) USING (people_id);
This is subtly different from two LEFT JOIN
s:
SELECT *
FROM people p
LEFT JOIN people_has_book pb USING (people_id)
LEFT JOIN books b USING (book_id);
The latter would show rows from people_has_book
even if there is no related entry in books
. However, in a classic many-to-many implementation with FK constraints enforcing referential integrity, there is typically no effective difference for your particular query, since all people_has_book.book_id
must reference an existing row in books
anyway - with the exotic exception of NULL values. (If (people_id, book_id)
is the PK of people_has_book
, both columns are NOT NULL
automatically.)
Related: