PHP SQL Joins Tutorial

Content: Inroduction of PHP SQL JOINs, SQL Left Join, SQL Right Join, SQL Full Join. MySQLi JOIN clause is used to combine rows from two or more tables, based on a related column between tow or more tables.

SQL have mainly 4 type of joins, below is list of sql joins and simple definition for it.

Types of SQL JOINs

  • LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
  • RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
  • FULL (OUTER) JOIN: Return all records when there is a match in either left or right table
  • JOIN (INNER): Returns records that have matching values in both tables
PHP SQL JOINs
PHP SQL Joins Tutorial

Some join examples


SELECT * FROM table1, table2;

SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;

SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;

SELECT * FROM table1 LEFT JOIN table2 USING (id);

SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id
LEFT JOIN table3 ON table2.id = table3.id;

Useful links for CodeIgniter