PHP AND MySQL - JOIN
Submitted by sudeepg on Tue, 07/31/2007 - 11:05.
JOIN in SQL combines records from 2 tables in a database. Often we might need to extract daata from 2 or more tables and then we need Joins. It simply is an act of joining 2 or more tables in a single temporary table.
Example:
DepartmentTable
| DepartmentId |
DepartmentName |
| 21 |
Sales |
| 32 |
Technical |
| 12 |
HR |
| 15 |
Clerical |
EmployeeTable
| EmployeeName | DepartmentId |
| Mohan |
32 |
| David |
12 |
| Bruce Lee |
15 |
| Jackie Chan |
12 |
| Tom cruise |
21 |
Assume we have 2 tables as mentioned above. And we need to list people in each department. We can do this using a simple Join as follows:
"Select EmployeeTable.EmployeeName, DepartmentTable.DepartmentName from EmployeeTable, DepartmentTable where EmployeeTable.DepartmentId = DepartmentTable.DepartmentId";
This is a "simple join" that fetches results from 2 tables and display it. The result is:
| EmployeeName |
DepartmentName |
| Mohan | Technical |
| David | HR |
| Bruce Lee | Clerical |
| Jackie Chan | HR |
| Tom cruise | Sales |
The Code in PHP for the same would be :
<?php |
This was a simple join used to fetch and display result from 2 tables. We'll now move to LEFT and Other Joins.

Recent comments
1 week 2 days ago
2 weeks 6 days ago
4 weeks 2 days ago
4 weeks 4 days ago
4 weeks 6 days ago
5 weeks 17 hours ago
29 weeks 3 days ago
30 weeks 3 hours ago
30 weeks 4 hours ago
30 weeks 2 days ago