JOIN Multiple Tables in PHP

 If we want to join multiple tables and table names are "personal", "city" and "courses" then we can use below shown sql query : 

In "personal" table, there are 6 columns named id, name, percentage, age, gender, city.

In "personal" table, column name "id" is PRIMARY KEY and column name "city" is FOREIGN KEY.


In "courses" table, there are 2 columns named course_id and course_name.

In "courses" table, column name "course_id" is PRIMARY KEY.


In "city" table, there are 2 columns named cid and cityname.

In "city" table, column name "cid" is PRIMARY KEY.


If we want to join above three tables then we can use below shown sql query :

SELECT * FROM personal INNER JOIN city ON personal.city = city.cid INNER JOIN courses ON personal.city = courses.course_id;


If we want to apply alias or short name for table name then we can use below shown sql query :

SELECT * FROM personal p INNER JOIN city c ON p.city = c.cid INNER JOIN courses cr ON p.city = cr.course_id;


If we want to select some particular columns records for tables then we can use below shown sql query :

SELECT p.id, p.name, p.percentage, p.age, p.gender, c.cityname, cr.course_name FROM personal p INNER JOIN city c ON p.city = c.cid INNER JOIN courses cr ON p.city = cr.course_id;


If we want to apply some condition for some particular columns records for tables then we can use below shown sql query :

SELECT p.id, p.name, p.percentage, p.age, p.gender, c.cityname, cr.course_name FROM personal p INNER JOIN city c ON p.city = c.cid INNER JOIN courses cr ON p.city = cr.course_id WHERE c.cityname = "Agra" ORDER BY p.name;

Comments

Popular posts from this blog

Logical_Operators

SubQuery with EXISTS and NOT EXISTS in PHP

Get Functions