Need help with a query that I wrote:
I have three tables
Company
id name
1 Gary's
Employee
id name company_id
1 Tim Jones 1
2 Sam Adams 1
reports to
employee_id reports_to_id
1 2
My current query is:
select
temp.company.name as comp_name,
temp.employee.name as employee_name,
temp.employee.id as employee_id
from temp.employee, temp.employee
where temp.company.id = temp.employee.company_id and temp.company.id = 1
Which gives me the output of:
comp_name employee_name employee_id
Gary's Tim Jones 1
I need something like this:
comp_name employee_name reports_to
Gary's Tim Jones Sam Adams
What’s a good way to modify my query to do this? I have a query and then I take those results and run a second query against that result set (which is excessively unnecessary).
Assuming an employee only reports to one person then we could have (no link table)
Then you could have a query similar to
If the employee reports to multiple people then we would use a link table