I have a search form used to query service provisions, which I will call 'provisions'. Provisions may have certain eligibility requirements in a has and belongs to many relationship, but they may also have no requirements at all. My issue is that when I exclude certain provisions on the basis of a particular requirement, those provisions that have no requirements at all are also excluded.
Table structure:
provisions
* id
* title
requirements
* id
* title
provisions_requirements
* provision_id
* requirement_id
Suppose a requirement is like:
Canadian Citizenship (id 4)
This would be presented on the form like: "Exclude services that have the requirement of: Canadian Citizenship".
The exclusionary portion of the query would thus be:
requirements.id NOT IN (4)
However, I then only get provisions that have at least one requirement, because those provisions that have no requirements are not included in the join.
The actual query is like:
SELECT DISTINCT `provisions`.id
FROM `provisions`
LEFT OUTER JOIN `provisions_requirements`
ON `provisions_requirements`.provision_id = `provisions`.id
LEFT OUTER JOIN `requirements`
ON `requirements`.id = `provisions_requirements`.requirement_id
WHERE (requirements.id NOT IN ('1'))
LIMIT 0, 10