Is there a way to write a parametric SQL query that matches notes linked to multiple projects without using HAVING ?
Say I have a "notes" table, a "projects" table and a "notes_projects" table linking these two together. I want to find the notes linked to all the project ids in the IN clause. Here is a version with HAVING:
SELECT notes.*, COUNT(np.project_id) AS np_count
FROM notes
INNER JOIN notes_projects AS np
ON np.note_id = notes.id
WHERE np.project_id IN (?)
GROUP BY notes.id
HAVING np_count = 2;
Is there a way to not use the HAVING clause or is this the correct way to handle such cases ?