Reorder JOINs to support older versions of SQLite.

In older versions of SQLite (e.g., 3.5.9), NATURAL JOIN conditions
between three or more tables must be ordered so that adjacent table
references share common column names.  If it's not possible to chain
table references together in this way, it may be necessary to use
the ON constraint to specify conditions explicitly.