The problem is when I run the code it checks to see if any person has been in department 1 then stops but only want persons that have been in all four departments
SELECT
p.person_id AS ID,
CONCAT(p.firstname, " ", p.surname) AS 'Employee Name'
FROM
person AS p,
allocation_to_department AS ad
WHERE
ad.person_id = p.person_id
AND ad.department_id= ('1,2,3,4')
3
Answers
You can do it by grouping the joined tables and select only the rows that have all 4
department_id
s:Here is an aggregate query that will return the persons that belong to all 4 departments (id 1 to 4):
Another solution would to be use an
IN
clause andCOUNT(DISTINCT ...)
:NB: always use explicit
JOIN
s instead of old-school, implicit joins. I modified the queries accordingly.Try this…