I have three different tables that I´d like to join, and I´d like the same attribute twice.
Let´s say we have a table for employees containing EmpID, Lastname, Firstname and Organisation.
We have another table for the organisation tree containing OrgID, Name, Level and ParentID.
So, let´s say that we have one row in this table where OrgID = 1, Name = Walmart, Level =1 and ParentID = -1.
Then we have another row which looks like this: OrgID = 2, Name = Kitchen, Level = 2 and ParentId = 1. ParentID decides where in the organisation tree our organisation called Kitchen is. Since parentid = 1 and orgid = 1 for Walmart, the organisation Kitchen is under Walmart, Walmart\Kitchen.
The third table, org_emp, combines our employees with the organisation tree.
We have just two attributes, OrgID and EmpID.
So let´s say our first employee has EmpID = 1 and OrgID = 2. That means he is working in the department Kitchen under Walmart, since these two tables join on OrgID. Let´s say this employee has EmpID = 1, lastname = Gretzky, firstname Douglas and OrgID = 2.
The result I´d like one single sql query to make is:
Douglas, Gretzky, Walmart, Kitchen.
So in one row I´d like both the employees current organisation (Kitchen) and under which organisation Kitchen resides, that is Walmart.
The tabels would look like this.
Organisation:
OrgID | Name | Level | ParentID
1 | Walmart | 1 | -1
2 | Kitchen | 2 | 1
Employees:
EmpID | Lastname | Firstname | Organisation
1 | Gretzky | Douglas | 2
org_emp
OrgID | EmpID
2 | 1