I have this simple table (called jobs_criterias
) :
+-----------+---------+-------------+
| id | job_id | criteria_id |
+-----------+---------+-------------+
| 101 | 4 | 3 |
| 105 | 6 | 5 |
| 130 | 8 | 5 |
| 132 | 9 | 5 |
| 133 | 6 | 7 |
| 150 | 7 | 8 |
| 160 | 8 | 9 |
+-----------+---------+-------------+
I want to find the job_id
which have same criteria_id
.
For example, I want to search job_id
for criteria_id IN (5, 7)
. It should return 6 with the sample data (only job_id
= 6 matches both criteria_ids
)
After some searchs, I use this query :
SELECT DISTINCT
job_id
FROM jobs_criterias jc1
INNER JOIN jobs_criterias jc2 USING(job_id)
WHERE jc1.criteria_id = 5 AND jc2.criteria_id = 7
It works but this query is not easily extensible. I use query builder from PHP framework and it's hard to build this kind of queries.
Is there any simpler way to achieve this ?