SQL left join queryWe can link more than one table to get the records in different combinations as per requirement. Keeping data of one area in one table and linking them each other with key field is better way of designing tables than creating single table with more number of fields. For example in a student database you can keep student contact details in one table and its performance report in another table. You can link these two tables by using one unique student identification number ( ID ).
Here are two tables with some data. We will apply first sql where command to this table
Using WHERE Query
The output of the above SQL command will give records of MySQL table t1 for which there is a record present in table t2 and linking is done by id field.
We will lern the difference betweeen WHERE condition and LEFT JOIN
USING LEFT JOIN
We can display all the records of table t1 along with the (matching ) records of t2 if matching is found by linking them through ID field. Here is the left join SQL to display this and below that the output is listed.
LEFT JOIN with IS NULLThe above result shows a simple left join and its output. We may be interested to identify the records in table t1 for which there is no matching record present in table t2. Here we have to use left join and link the tables by id field.
Here is the query using left join and the output is below that.
for MySQL 5 and above try like this
In the above result we have seen how the output works when we have to get records which are not present in other table. Now let us find out the other way , we will get the record of table one for which matching record exists in table 2. Here is the left join sql and below that the results of the query.
for MySQL 5 try like this
PHP script using left join to display recordsWe will try to develop one PHP script to display records by using left join query. Here we have used PHP PDO connection to connect to MySQL database. You can read more on PHP connection to MySQL using PDO here.
After connection we have used foreach loop to display all the records. Here is the code.
We can replace the query part in above code to check other results.
Here is the SQL dump of two tables t1 & t2
Application of LEFT Join QueryWe have two tables, one is the student list and the other one is a selected football team. Some of the students are selected to play for the football team. In our football table, we have kept two student ids (selected for the football team ).
Using left join we can list out various way to display the list.
From the above list you can see by using WHERE condition we can filter out our required records
Using three tables in LEFT Join QueryIn above example we have seen how to find out records present in one table and not present in another table. Let us try for another example.
In addition to football team there is one more baseball team. Two students are selected for baseball team. So now we have two in football team and two in baseball team.
Display all students with a mark for those who are selected for teams ( any )
Next, let us find out who are the students not selected in any of the team. In other words, find out the records in student table that don't have matching records in football or baseball table ( team )
We can get similar result by using UNION , NOT IN and subqueries.
You can read more Null data here .
Exercise For better understanding of LEFT JOIN , check the sample code for generating reports on Sales.
This article is written by plus2net.com team.