In a MySQL JOIN, what is the difference between ON and USING()? As far as I can tell, USING() is just more convenient syntax, whereas ON allows a little more flexibility when the column names are not identical. However, that difference is so minor, you’d think they’d just do away with USING().
Is there more to this than meets the eye? If yes, which should I use in a given situation?
It is mostly syntactic sugar, but a couple differences are noteworthy:
ON is the more general of the two. One can join tables ON a column, a set of columns and even a condition. For example:
USING is useful when both tables share a column of the exact same name on which they join. In this case, one may say:
An additional nice treat is that one does not need to fully qualify the joining columns:
To illustrate, to do the above with ON, we would have to write:
Notice the
film.film_idqualification in theSELECTclause. It would be invalid to just sayfilm_idsince that would make for an ambiguity:As for
select *, the joining column appears in the result set twice withONwhile it appears only once withUSING: