In This Tutorial
MySQL left join
In order to use joins in MySQL, we normally use at least two tables to fetch data. One table name is given after the select statement while the other table name is given after using the type of join. The table with the select statement is left while the one comes after join clause is taken as the right table.
In the MySQL left join, all records from the left table are fetched while from the right table only those rows are fetched that matches the given common key.
The opposite is to left join type is the right join. As such, the left join returns all rows of the left table, the values in the right table will be visible as Null that are not matched.
Left join syntax
This is how left join is used in MySQL queries:
Select * from table_name1
left Join table_name2 ON table_name1.common_field = table_name2.common_field
A left join example
Following is an example of using the MySQL left join where we will use two tables. The table tbl_employee keeps the records of employees in a departmental store database. The other table, tbl_products stores products while tbl_customers_order keeps the records of orders placed by customers. In tbl_customers_order table a common key, emp_id exists that relates to tbl_emploee. The purpose of this key is to maintain the integrity of data so that only an employee/operator which information exists in the tbl_employee can take an order. The tables are:
To perform a left join, we will fetch all records from the tbl_employee table that will be towards left side (with the select statement). While table towards the right will be tbl_customer_orders, after the left join clause.
See the left join query and its output by clicking the link below:
The MySQL left join query:
select * from tbl_employee TE left join tbl_customers_orders TC ON TE.emp_id=TC.emp_id
You can see, all rows are fetched from tbl_emploee whereas columns for tbl_customers_orders are filled with Null values.
Also See: MySQL Joins