Note that there are some explanatory texts on larger screens.

plurals
  1. POselecting based on all conditions being met (relational division)
    text
    copied!<p>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.<br/> This is the kind of table they see:</p> <pre> +-----------------------+---------------------+------------+ | Topics | Questions available | Selection: | +-----------------------+---------------------+------------+ | health,safety,general | 13 | | | health | 3 | | | safety | 7 | | | general | 1 | | +-----------------------+---------------------+------------+ </pre> <p>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.<br/> 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:</p> <pre> select questionid from question_has_topic where not exists ( select questionid from question_has_topic where topicid not in (8,9,10)) </pre> <p>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 <a href="http://luhong.wordpress.com/2006/03/05/relational-division-in-sql/" rel="nofollow">link</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload