根据满足所有条件进行选择(关系划分)
我有表 Question、topic 和 Question_has_topic (多对多关系)。在我的应用程序中,管理员可以看到按主题分组的问题细分,他们可以选择希望系统随机选择的问题来创建测试。
这是他们看到的表格类型:
+-----------------------+---------------------+------------+ | Topics | Questions available | Selection: | +-----------------------+---------------------+------------+ | health,safety,general | 13 | | | health | 3 | | | safety | 7 | | | general | 1 | | +-----------------------+---------------------+------------+
对于特定的主题分组,计数是唯一的。不管怎样,一旦他们做出选择,我需要一个 SQL 语句来选择与给定主题分组相对应的问题。 即,我可能需要 3 个问题,主题为健康、安全和一般。
我在网上做了一些研究,我认为我想做的就是关系代数中的除法,这是我的尝试topicid 的任意分组:
select questionid from question_has_topic where not exists ( select questionid from question_has_topic where topicid not in (8,9,10))
结果为空,尽管数据库中有 2 个问题包含所有这些主题 id,这告诉我这不起作用。我正在按照此链接中的示例进行操作
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:删除了我的旧帖子,因为我误读了问题。
这是我过去使用过的一种技术:
其中
3
对应于给定组中的主题数量。这假设question_has_topic
中的每个(questionid, topicid)
对都是唯一的(它应该位于多对多关系表中)。此查询的工作方式是,首先选择至少分配了一个所需主题的任何问题 (
WHERE qht.topicid IN (8,9,10)
),然后按 < 进行分组代码>问题ID。仅当给定问题分配了全部三个主题时,第一个 HAVING 子句 (COUNT(*) = 3
) 才为 true(因为我们假设此表中不允许出现重复项)。第二个 HAVING 子句检查分配给问题的主题总数。这是为了防止出现这样的情况,例如,某个问题可能分配有主题 8、9、10 和 11。EDIT: removed my old post since I misread the question.
This is a technique I've used in the past:
where
3
corresponds to the number of topics in the given group. This assumes that each(questionid, topicid)
pair inquestion_has_topic
is unique (which it should be in a many-to-many relationship table).The way this query works is by first selecting any question that has at least one of the desired topics assigned to it (
WHERE qht.topicid IN (8,9,10)
), then grouping by thequestionid
. The first HAVING clause (COUNT(*) = 3
) can only be true if a given question has all three topics assigned to it (since we assume duplicates aren't allowed in this table). The second HAVING clause checks the total number of topics that are assigned to the question. This is to guard against the case where, for example, a question may have topics 8, 9, 10, and 11 assigned to it.我认为这就是您想要写的内容,但这是一种非常低效的方法 -
这肯定要快得多 -
更新:我知道有一个更简单的答案。 Cheran 的方法要简单得多,并且运行速度应该比 INNER JOIN 稍快一些。请接受他的回答。
I think this is what you were trying to write but it is a very inefficient way of doing it -
This is definitely much faster -
UPDATE: I knew there was a simpler answer. Cheran's method is much simpler and should run slightly faster than the INNER JOINs. Please accept his answer.