I am trying to write a query that will let me search if a patient has NOT received a medication. At first I thought that I could just do something like this:
INNER JOIN prescript p ON p.id = patient.id AND p.med_id NOT IN (5128)
Where p.id
and patient.id
signify the patient id, p.med_id
refers to the medication id and 5128
is the medication being used in the query. The problem with this query is that if the patient has had other medications, those will show up. So if a patient has had medication 5128, but also another medication they will show up. But I only want to return a patient if they have not have that medication at all. The part that is tricky here is that I won't be able to know the patient id in any of this. So I need a solution that is similar to this:
INNER JOIN prescript p ON p.id = patient.id AND (SELECT med_id
FROM prescript
WHERE id = p.pid) NOT IN (5128)
I was thinking that this might be possible if I could concatenate all of the med_id's into one string as well, but my problem with that is that I'm using a query builder so I'm limited to adding joins and adding wheres only. Aka I can't use DECLARE
.