MySQL Joins: ON vs USING
MySQL provides two ways to perform joins: ON and USING. Understanding the subtle differences between these two syntaxes can help optimize your queries.
ON
ON is the more versatile option. It allows you to join tables on a single column, multiple columns, or even a condition. For instance, you can join tables based on a specified range of values or a logical expression:
SELECT * FROM world.City JOIN world.Country ON (City.CountryCode = Country.Code) WHERE City.Population > 1000000
USING
USING is a simplified syntax specifically designed for cases where the tables to be joined share an exact column name. Instead of specifying the column name in the ON clause, you simply use the USING keyword followed by the shared column name:
SELECT ... FROM film JOIN film_actor USING (film_id) WHERE ...
Notable Differences
Choosing the Right Option
Generally, USING is a more convenient option when the tables share an exact column and qualify both columns in the query. However, if you need to join tables on a condition or multiple columns, ON is the preferable choice.
The above is the detailed content of MySQL Joins: ON vs. USING: When Should I Use Which?. For more information, please follow other related articles on the PHP Chinese website!