Joining Tables in MySQL: Tricky Scenario

Here’s a way to join tables in MySQL when linking field name is different

ON THIS PAGE

  1. MySQL Join query
  2. Steps in a join query
  3. The table-period-field approach
  4. Points to remember

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

Related

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.