I have a couple of tables i want to join. They are connected through a pivot table.
I have a projects
table, a employees
table and a pivot table called employee_project
that has both a project_id
and a employee_id
:
id | employee_id | project_id |
---|
I want to get the first_name
and last_name
from the employees
table but the where clause should search in the company_id
and course_id
on the projects
table.
So far i have:
SELECT *
FROM projects
JOIN employees
ON employees.id = employee_project.employee_id
JOIN employee_project
ON projects.id = employee_project.project_id
WHERE course_id = 3 AND company_id = 150004
But this obviously isn’t working as expected.
2
Answers
The issue in your query is that you’re using
employee_project
table before joining it. I just changed the order of join and it seems working!This should give you the
first_name
andlast_name
of employees who are associated with projects that meet your criteria.you should join employee_project table first, and then join employees table