I have two tables, entities and components, each with a name and unique ID. There is a third table mapping the many-to-many relationship between these two IDs called construction, containing the two columns, entity_ID and component_ID. An entity is composed of some number of components. So if the construction table looks like this:
+----------+---------------+
|entity_id | component_id |
+----------+---------------+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
| 2 | 6 |
+----------+---------------+
Entity 1 is made out of components 1,2, while entity 2 is made out of 2 and 6. The application I’m working on also has an available table containing the IDs of all available components. My question is this, how do I query the database to return the entities constructed strictly of components in the available table? In the given example, if 1,2 and 6 are all in the available table, then entities 1 and 2 should be returned. Otherwise, if 2 isn’t in the available table (but 1 and 6 are) then nothing is returned. I’m new to MySQL so if you could explain the logic as well that would be great.
I tested @Abdullah’s subquery and indeed got the opposite of what I wanted. After a little testing I ended up with the nested queries that give me what I was looking for: