How to LEFT JOIN on Multiple Columns in MySQL

MD Aminul Islam Feb 15, 2024
How to LEFT JOIN on Multiple Columns in MySQL

Sometimes when working with large databases, you may need to merge different fields from different tables into one table. The term we will use for this purpose is the left join.

In this quick article, we’re going to see how we can make a left join in MySQL, and also we will take a look at a relevant example and explain it part by part to make the topic easier.

Use LEFT JOIN in MySQL

The rule of the left join is that it will only show all the data from the left table and match data from the right table.

In our example below, we’ll see how we can make a left join in MySQL. For the example, we are going to use the below two tables,

Table 1:

Project_ID Project_name Members

Table 2:

ID Name Hours Minute Seconds

Now let’s take a look at the following query,

SELECT
projects.Project_ID,
projects.Members,
project_status.Name
FROM projects LEFT JOIN project_status ON projects.Project_ID = project_status.ID

In the query above, we first selected all the fields of the left table we wanted to merge and, based on the condition, took the data from the right on the selected field. Here we use the keyword LEFT JOIN to create a connection between two tables, and the keyword ON is used to apply the condition.

After executing the above example query, you will get the below output.

LEFT JOIN on Multiple Columns in MySQL

There must be a common field between the two tables to match the condition to make the connection between them.

Please note that the query we used in this article is written in MySQL.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - MySQL Join