I have 3 tables below. I want to create a views table joining with three tables. Example:
Employee Table
| EID | Name |
|---|---|
| 001 | Mr. Devid |
| 002 | Mr. Kamal |
Location Table
| LID | Location |
|---|---|
| 1 | Dhaka |
| 2 | Khulna |
| 3 | Rangpur |
Tour Table
| EID | DepartureID | ArrivalID |
|---|---|---|
| 001 | 1 | 2 |
| 002 | 3 | 1 |
My Views Table
| EID | Name | Departure | Arrival |
|---|---|---|---|
| 001 | Mr. Devid | Dhaka | Khulna |
| 002 | Mr. Kamal | Rangpur | Dhaka |
How can i create the View table?
Gurpreet AroraPosted Sep 25, 2024, 4:19 AM
Query :
CREATE VIEW MyViewsTable AS SELECT e.EID, e.Name, d.Location AS Departure, a.Location AS Arrival FROM Employee e JOIN Tour t ON e.EID = t.EID JOIN Location d ON t.DepartureID = d.LID JOIN Location a ON t.ArrivalID = a.LID;
Explanation:
Employee e: This represents theEmployeetable (aliased ase).Tour t: This represents theTourtable (aliased ast), which linksEmployeewithLocationthroughEID.Location d: This is the first join with theLocationtable (aliased asd), which matches theDepartureIDwithLIDto get the departure location.Location a: This is the second join with theLocationtable (aliased asa), which matches theArrivalIDwithLIDto get the arrival location.CREATE VIEWstatement defines the viewMyViewsTableto store this result.SELECT * FROM MyViewsTable;
Vishal JoshiPosted Sep 25, 2024, 5:26 AM
Hello Debashish,
You need to use INNER JOIN To get the expected data. to create view you can use the below SQL.
Thanks
Vishal Joshi