SQLJoins intends combining information from 2 aliases much tables, based connected a related column. A array subordinate creates a impermanent array showing the information from the joined tables.
For example, we person 2 tables that follow, the array named tblcustomers stores the accusation astir the customer
id | Name | Address | RegDate |
1 | John Doe | New Delhi | 2024-01-02 |
2 | Anuj kumar | Noida UP | 2024-04-01 |
3 | Amit Singh | Bangalore Karnataka | 2024-05-01 |
4 | Alex | Chennai Tamilnadu | 2024-06-09 |
The tbloders array shop the accusation astir the individual orders pinch their corresponding amount:
id | Customer_Id | Product_Name | Amount |
1 | 1 | iPhone | 920000 |
2 | 2 | Sony TV | 45623 |
3 | 2 | Laptop | 85641 |
4 | 3 | Book | 456 |
5 | 4 | Toys | 1452 |
Rather than storing the customer sanction successful some tables, the tblorders array contains a reference to the customer id that appears successful tblcustomers table. We will usage subordinate to prime the corresponding information from some tables.
Syntax:
1 2 3 4 5 |
SELECT <ColumnName1>,<ColumnName2>,<ColumnNameN> From <TableName1> Join <TableName2> on <TableName1.ColumnName1>=<TableName2.ColumnName2> Where <Condition> Order By <ColumnName1>,<ColumnName2>,<ColumnNameN>; |
In the Above Syntax:
- ColumnName1 successful TableName1 is usually that table’s Primary key.
- ColumnName2 successful TableName2 is simply a Foreign Key successful that table.
- ColumnName1 and ColumnName2 must person the aforesaid Data Type and for definite information types, the aforesaid size.
Example utilizing the supra 2 tables tblcustomers and tblorders
1 2 3 4 |
SELECT tblcustomers.id, tblcustomers.Name, tblcustomers.Address, tblorders.Product_Name, tblorders.Amount FROM tblcustomers join tblorders on tblorders.Customer_id=tblcustomers.id ORDER BY tblcustomers.id; |
Output Will be:
id | Name | Address | Product_Name | Amount |
1 | John Doe | New Delhi | iPhone | 920000 |
2 | Anuj kumar | Noida UP | Sony TV | 45623 |
2 | Anuj kumar | Noida UP | Laptop | 85641 |
3 | Amit Singh | Bangalore Karnataka | Book | 456 |
4 | Alex | Chennai Tamilnadu | Toys | 1452 |