Here’s a way to join tables in MySQL when linking field name is different
ON THIS PAGE
MySQL Join query
The SQL query is to join Customer and Country tables.
SELECT country_Name
FROM customer
JOIN country
ON customer.country = country.countryISO2;
Steps in a join query
First
Enter the name of the first table to join after the FROM keyword.
Second
Add the JOIN keyword after the table name.
Third
Enter the name of the second table to join.
Then
Add the ON keyword.
Finally
Enter the names of the two fields (the link between tables), separated by the equal (=) sign.
The table-period-field approach
The Customer table has a country field. But the Country table does not have the same field name. So in the ON condition, you give filed names following the syntax table-period-field.
Points to remember
- Use the table-period-field approach when the field names are different. It is a best practice.
- The order of the field names in the ON clause is irrelevant; they do not have to be in the same order as the tables in the query.
References
- The Definitive Guide to MySQL
- Learning MySQL: Get a Handle on Your Data
- MySQL for Beginners from Nanyang Technological University
Related