连接:三个表和一个或条件
我想我应该以某种方式知道这一点,特别是在阅读了很多有关“条件必须进入 ON 子句,而不是 WHERE 子句”的问题和答案之后。然而,我还是迷失了。
我有三个表,通常使用左(外部)连接来连接它们。连接的表看起来像这样(retty 标准):
task_id task_questions_taskId taskQuestions_questionId question_id 1 1 5 5 1 1 8 8 2 2 8 8
SELECT `t`.`id` AS `task_id` ,
`task_questions`.`taskId` AS `task_questions_taskId` ,
`task_questions`.`questionId` AS `task_questions_questionId` ,
questions.id AS question_id
FROM `task` `t`
LEFT OUTER JOIN `task_questions` `task_questions`
ON ( `task_questions`.`taskId` = `t`.`id` )
LEFT OUTER JOIN `question` `questions`
ON ( `task_questions`.`questionId` = `questions`.`id` )
这是获取所有记录的标准查询。 (它取自 Yii;我实际上想使用 Active Record 来实现这一点,但甚至无法获得简单的 SQL)。
现在我只想获取那些具有 Question_id 2 和 8 的任务(例如) 因此,如果任务不具有这两个 Question.ids,我不希望它出现在结果集中。 在这种情况下,任务也可以有其他question_id。尽管如果查询只返回那些恰好具有这 2 个(或任何其他集合)的查询,那么看看查询会是什么样子会很有趣。 很容易获得所有有一个问题的任务,使用 WHERE Question.id = 2, 但 WHERE 子句中的 AND 会导致空结果。
I think I should know this somehow, especially after reading a lot of questions and answers regarding "The condition must go into the ON clause, not in the WHERE clause". However, I am still lost.
I have three tables, and I join them normally with LEFT (OUTER) joins. The joined tables looks like this (retty standard):
task_id task_questions_taskId taskQuestions_questionId question_id 1 1 5 5 1 1 8 8 2 2 8 8
SELECT `t`.`id` AS `task_id` ,
`task_questions`.`taskId` AS `task_questions_taskId` ,
`task_questions`.`questionId` AS `task_questions_questionId` ,
questions.id AS question_id
FROM `task` `t`
LEFT OUTER JOIN `task_questions` `task_questions`
ON ( `task_questions`.`taskId` = `t`.`id` )
LEFT OUTER JOIN `question` `questions`
ON ( `task_questions`.`questionId` = `questions`.`id` )
This is the standard query to get all the records. (It's taken from Yii; I actually want to to this with Active Record, but can't even get plain SQL right).
And now I want to get ONLY those tasks that have the question_id 2 AND 8 (e.g)
So if a task has not both of those question.ids, I don't want it in the result set.
In this case, the task could have other question_ids, too. Although it would be interesting to see how the query would look if it should return only those that have exactly those 2 (or any other set).
It's easy to get all the tasks that have one question, with WHERE question.id = 2,
but an AND in the WHERE clause leads to an empty result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
WHERE 子句一次只能将条件应用于一行。但是您不同 id 的问题出现在不同的行上。怎么解决这个问题呢?使用自连接将两行连接到一行。
下面是一个示例:
另一个解决方案是查找具有问题 2 OR 8 的任务,然后使用 GROUP BY 和 HAVING 按正好包含其中两个问题的组进行筛选。
The WHERE clause can only apply conditions to one row at a time. But your questions of different id occur on different rows. How to solve this? Join both rows onto one row using a self-join.
Here's an example:
Another solution is to find the tasks that have questions 2 OR 8, and then use GROUP BY and HAVING to filter by groups that have exactly two of those.
即使不使用和,你也可以做到这一点
...其中 Question.id IN (2,8)
you can do this even with out using and
... where question.id IN (2,8)
使用
IN
:Use
IN
:这应该可以做到
This should do it
您不是在寻找 AND,而是在寻找 OR 或 IN:
或者
如果您使用
AND
,则意味着 ID 必须同时为 8 和 2。糟糕的交易。You're not looking for AND, you're looking for OR, or an IN:
Or
If you use
AND
that would mean the ID would have to be 8 and 2 at the same time. Bad deal.