INNER JOIN tutorial in php
Types of JOINS in MySQL :
INNER JOIN,
LEFT JOIN,
RIGHT JOIN,
CROSS JOIN
INNER JOIN : The INNER JOIN selects records that have matching values in both tables.
NOTE : INNER JOIN and JOIN both works same in query. We can simply write INNER JOIN to JOIN only.
INNER JOIN Syntax :
SELECT columns FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
If we want to perform INNER JOIN Operation on Table name "city" and "personaltwo" in which column name "cid" is PRIMARY KEY in table name "city" and column name "city" is FOREIGN KEY in table name "personaltwo" then we can use below sql query :
In "personaltwo" table, column "city" is FOREIGN KEY and In "city" table, column "cid" is PRIMARY KEY.
SELECT * FROM personaltwo INNER JOIN city ON personaltwo.city = city.cid;
If we want to use short name of table name like "p" for table name "personaltwo" and "c" for table name "city" in above sql query then we can use below shown sql query with the same result as above query :
SELECT * FROM personaltwo p INNER JOIN city c ON p.city = c.cid;
If we want to select some specific column from table name "personaltwo" and "city" then we can use below shown sql query :
SELECT p.id, p.name, p.percentage, p.age, p.gender, c.cityname FROM personaltwo p INNER JOIN city c ON p.city = c.cid;
If we want to select records with some specific condition like cityname = "Agra" from table name "personal" and table name "city" using INNER JOIN then we can use below shown sql query :
SELECT p.id, p.name, p.percentage, p.age, p.gender, c.cityname FROM personaltwo p INNER JOIN city c ON p.city = c.cid WHERE c.cityname = "Agra";
If we want to arrange above query records in Ascending order using ORDER BY then we can use below sql query :
SELECT p.id, p.name, p.percentage, p.age, p.gender, c.cityname FROM personaltwo p INNER JOIN city c ON p.city = c.cid WHERE c.cityname = "Agra" ORDER BY p.name;
Comments
Post a Comment