I have the following MySQL query:
SELECT tbPacientesComunicacao.id
FROM tbPacientesComunicacao
WHERE tbPacientesComunicacao.idRefUser = 1
AND tbPacientesComunicacao.idRefPaciente = 4
AND tbPacientesComunicacao.id IN (
SELECT idRefComunicacao
FROM tbPacientesComunicacaoDestinatarios
WHERE idRefUser IN (2,8)
);
The issue is that MySQL returns the ID if either 2 or 8 matches… how can I say hey only return the id if both numbers (2 and 8) matches?
Table tbPacientesComunicacao
id | description |
---|---|
1 | Something |
Table tbPacientesComunicacaoDestinatarios
idRefComunicacao | UserID |
---|---|
1 | 5 |
1 | 7 |
When a new record is being inserted, I have to check if that record doesn’t match any of the communications already in the database. So I pass the user IDs in an array(5,8) then I have to check it somehow if there is something in the database the matches EXACTLY this combination of UserIDs, in this test it shouldn’t match because I have 5 and 7 not 5 and 8 but using "in" it says it was matched because it found at least one of them… how can I determine that only matches if all numbers in the array matches the table userID?
2
Answers
I found a solution that I was trying to avoid but since using a MySQL query seems to be really hard... this works using PHP and MySQL:
I think you can do something like:
This way you only select the
idRefComunicacao
for whichidRefUser IN (2,8)
and there are exactly two results (the one withidRefUser
2 and the one with 8)