I have two tables: one which contains items on a checklist (checklist_id, content), and one which contains a relation between users and checklist item numbers (checklist_fk, user_fk). Every time a user checks an item off, a row is inserted indicating that user 15 completed item 3.
When I query this, I’m using
SELECT content, user_fk, checklist_id, checklist_fk
if (checklist_fk is null, false, true) completed
FROM checklist
LEFT JOIN checklist_completion ON checklist_id = checklist_fk;
However, completed returns true if ANY user has completed a given checklist item. I need to filter the second table WHERE user_fk = XX before the join, but how do I do that? I believe you’re supposed to use HAVING user_fk = XX but that throws a syntax error.
If you’re targeting a specific user, you have to add that condition inside the
LEFT JOINlike so:Btw, as you might have noticed, I’ve taken out the
IFstatement because it’s easy to check forNULLcondition inside your code.