I have the tables question, topic and question_has_topic (many-to-many relationship). In my application admins see a breakdown for questions grouped by their topics and they select how many from each they'd like the system to randomly select to create a test.
This is the kind of table they see:
+-----------------------+---------------------+------------+ | Topics | Questions available | Selection: | +-----------------------+---------------------+------------+ | health,safety,general | 13 | | | health | 3 | | | safety | 7 | | | general | 1 | | +-----------------------+---------------------+------------+
The count is unique for the particular grouping of topics. Anyway, once they make the selection I need a SQL statement which will select questions that correspond to the given grouping of topics.
I.e. I might need 3 questions which have the topics health,safety and general.
I was doing some research online and I think that what I'm trying to do is known as divide in relational algebra and here is my attempt for an arbitrary grouping of topicids:
select questionid from question_has_topic where not exists ( select questionid from question_has_topic where topicid not in (8,9,10))
The result is empty, although there are 2 questions in the database that have all these topic ids which tells me this isn't working. I was following the example from this link