There must be a better way…
Hello all, I am now designing a new data structure and I faced this problem that I am going to describe making me think that maybe I should redesign it. Maybe the solution is in front of my eyes, and please if you have any ideas help me figure this out.
Lets say that we have a table persons_records
(table and data in this example made as simple as possible)
ID PERSON_ID RECORD_ID
1 1 1
2 1 2
3 2 3
4 2 2
5 2 1
6 3 1
7 3 3
What I want is to get all the PERSON_ID s that have for example the RECORD_ID 1 and the RECORD_ID 2
The solution I came up is
SELECT a.PERSON_ID FROM persons_records as a JOIN persons_records as b WHERE a.RECORD_ID = 1 AND b.RECORD_ID = 2 AND a.PERSON_ID = b.PERSON_ID GROUP BY a.PERSON_ID
And it works but … I can’t stop thinking that there must be a better (and maybe more generic) way…
Thank you in advance for your ideas.