+ All Categories
Home > Education > 13-Mysql joins php

13-Mysql joins php

Date post: 15-Jan-2015
Category:
Upload: raja-kumar
View: 800 times
Download: 2 times
Share this document with a friend
Description:
 
Popular Tags:
14
MySQL Joins MySQL Joins
Transcript
Page 1: 13-Mysql joins php

MySQL JoinsMySQL Joins

Page 2: 13-Mysql joins php

MySQL Joins OverviewMySQL Joins Overview MySQL Join is used to join the records from MySQL Join is used to join the records from

two tables using join clause.two tables using join clause.

The Join Clause return you the set of The Join Clause return you the set of records from both table on the basis of records from both table on the basis of common column.common column.

Page 3: 13-Mysql joins php

MySQL Join TypesMySQL Join Types MySQLMySQL Inner JoinInner Join MySQL Equi JoinMySQL Equi Join MySQL Natural JoinMySQL Natural Join MySQL Cross JoinMySQL Cross Join MySQL Outer JoinMySQL Outer Join

Left Outer JoinLeft Outer Join Right Outer JoinRight Outer Join

Self JoinSelf Join

Page 4: 13-Mysql joins php

Inner joinInner join produces only the set of  produces only the set of records that match in both Table A and records that match in both Table A and Table B.Table B.

MySQL Inner JoinMySQL Inner Join

Page 5: 13-Mysql joins php

The INNER JOIN keyword returns rows The INNER JOIN keyword returns rows when there is at least one match in both when there is at least one match in both tables.tables.

If there are rows in "Persons" that do not If there are rows in "Persons" that do not have matches in "Orders", those rows will have matches in "Orders", those rows will NOT be listed.NOT be listed.

Example:Example: SELECT Persons.LastName, SELECT Persons.LastName, Persons.FirstName, Orders.OrderNoPersons.FirstName, Orders.OrderNoFROM Persons INNER JOIN OrdersFROM Persons INNER JOIN OrdersON Persons.P_Id = Orders.P_IdON Persons.P_Id = Orders.P_IdORDER BY Persons.LastNameORDER BY Persons.LastName

(Contd:)MySQL Inner JoinMySQL Inner Join

Page 6: 13-Mysql joins php

MySQL Outer JoinMySQL Outer Join MySQL Outer Join return you the set of all MySQL Outer Join return you the set of all

matching records from both table. matching records from both table. The Outer Join does not requires each The Outer Join does not requires each

records to be matched in both the tables.records to be matched in both the tables. MySQL Outer Join is categorized into two MySQL Outer Join is categorized into two

groups.groups. MySQL Left Outer JoinMySQL Left Outer Join MySQL Right Outer JoinMySQL Right Outer Join

Page 7: 13-Mysql joins php

MySQL Left Outer JoinMySQL Left Outer Join Left outer joinLeft outer join produces a complete set of  produces a complete set of

records from Table A, with the matching records from Table A, with the matching records (where available) in Table B. If there is records (where available) in Table B. If there is no match, the right side will contain null.no match, the right side will contain null.

Page 8: 13-Mysql joins php

MySQL Left Outer JoinMySQL Left Outer Join The left join is used in case of need to The left join is used in case of need to

return all rows from the left table, even if return all rows from the left table, even if the right table doesn't have any match.the right table doesn't have any match.

Example:Example: SELECT Persons.LastName, SELECT Persons.LastName, Persons.FirstName, Orders.OrderNoPersons.FirstName, Orders.OrderNoFROM Persons LEFT JOIN OrdersFROM Persons LEFT JOIN OrdersON Persons.P_Id=Orders.P_IdON Persons.P_Id=Orders.P_IdORDER BY Persons.LastNameORDER BY Persons.LastName

(Contd:)

Page 9: 13-Mysql joins php

MySQL Right Outer JoinMySQL Right Outer Join The right join is used in case of need to The right join is used in case of need to

return all rows from the right table, even if return all rows from the right table, even if the left table doesn't have any match.the left table doesn't have any match.

Example:Example: SELECT Persons.LastName,Persons.FirstName, SELECT Persons.LastName,Persons.FirstName, Orders.OrderNoOrders.OrderNoFROM Persons RIGHT JOIN OrdersFROM Persons RIGHT JOIN OrdersON Persons.P_Id = Orders.P_IdON Persons.P_Id = Orders.P_IdORDER BY Persons.LastNameORDER BY Persons.LastName

Page 10: 13-Mysql joins php

MySQL Cross JoinMySQL Cross Join Cross Join is also called Cartesian Product Cross Join is also called Cartesian Product

Join.Join. The Cross Join in SQL return you  a result The Cross Join in SQL return you  a result

table in which each row from the first table table in which each row from the first table is combined with each rows from the is combined with each rows from the second table.second table.

Page 11: 13-Mysql joins php

MySQL Cross JoinMySQL Cross Join In other words, you can say it is the cross In other words, you can say it is the cross

multiplication of number of rows in each multiplication of number of rows in each table.table. 

Example:Example: SELECT * FROM persons cross join orders;SELECT * FROM persons cross join orders;

(Contd:)

Page 12: 13-Mysql joins php

MySQL Equi JoinMySQL Equi Join Equi Join is a classified type of Inner Join in Mysql.Equi Join is a classified type of Inner Join in Mysql. Equi Join is used to combine records from two Equi Join is used to combine records from two

table based on the common column exists in table based on the common column exists in both table.both table.

The Equi Join returns you only those records The Equi Join returns you only those records which are available in both table on the basis of which are available in both table on the basis of common primary field name.common primary field name.

Example:Example: SELECT persons.firstname,orders.orderNo SELECT persons.firstname,orders.orderNo

FROM persons, ordersFROM persons, orders

WHERE persons.p_id = orders.p_id;WHERE persons.p_id = orders.p_id;

Page 13: 13-Mysql joins php

MySQL Natural JoinMySQL Natural Join MySQL Natural Join is a specialization of MySQL Natural Join is a specialization of

equi-joins.equi-joins. The join compares all columns in both The join compares all columns in both

tables that have the same column-name in tables that have the same column-name in both tables that have column name in the both tables that have column name in the joined table.joined table.

Example:Example: SELECT persons.firstname, orders.orderNoSELECT persons.firstname, orders.orderNo

FROM persons NATURAL JOIN orders;FROM persons NATURAL JOIN orders;

Page 14: 13-Mysql joins php

MySQL Self JoinMySQL Self Join These join allow you to retrieve related These join allow you to retrieve related

records from the same table. records from the same table.  The most common case where you'd use a The most common case where you'd use a

self-join is when you have a table that self-join is when you have a table that references itself.references itself.

Example:Example: SELECT m.name as "Manager", p.name as SELECT m.name as "Manager", p.name as "Employee“"Employee“

FROM employee m, employee pFROM employee m, employee p

WHERE m.emp_id = p.manager_id;WHERE m.emp_id = p.manager_id;


Recommended