mySQL SELECT FROM 表 WHERE ... AND ... AND ... AND
我有一个包含列和数据的表“文章”:
article_id title body
1 This is the title This is the body text
2 Another title Another body text
另一个包含列和数据的表“类别”:
category_id category
1 localnews
2 visible
3 first10
还有一个包含列和数据的表“类别”:
categories_id article_id category_id
1 1 1
2 1 2
3 1 3
4 2 1
5 2 3
我想选择行 WHEREcategories.category_id = 1 AND = 2 AND =3
我正在使用:
SELECT articles.article_id, articles.title, articles.body,
categories.article_id, categories.category_id
FROM articles, categories
WHERE articles.article_id = categories.article_id
AND categories.article_id = 1
AND categories.article_id = 2
AND categories.article_id = 3
但它不起作用。显然 mySQL 需要另一种语法。 有人可以帮忙吗? 谢谢
I have a table "articles" with columns and data:
article_id title body
1 This is the title This is the body text
2 Another title Another body text
Another table "category" with columns and data:
category_id category
1 localnews
2 visible
3 first10
And a table "categories" with columns and data:
categories_id article_id category_id
1 1 1
2 1 2
3 1 3
4 2 1
5 2 3
I want to SELECT the row(s) WHERE categories.category_id = 1 AND =2 AND =3
I'm using:
SELECT articles.article_id, articles.title, articles.body,
categories.article_id, categories.category_id
FROM articles, categories
WHERE articles.article_id = categories.article_id
AND categories.article_id = 1
AND categories.article_id = 2
AND categories.article_id = 3
but it doesn't work. Obviously mySQL needs another syntax.
Can someone help?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我对表使用了一些不同的名称,因为在您的示例中,很难注意到
category
和categories
之间的区别。I used a bit different names for table because in your example the distinction between
category
andcategories
is hard to notice.一行的列不能同时为1、2或3,这是
AND
规定的。在WHERE
条件中使用OR
。更好的是 - 为了便于阅读 - 您可以使用IN
:An column of a row cannot be 1, 2 or 3 at the same time, which is what
AND
stipulates. UseOR
in yourWHERE
condition. Better yet - for readability - you can useIN
:除了常用的 IN() 和使用 HAVING 计数之外,我还对通过执行如下多重连接所带来的性能差异感兴趣...
是的,这可能看起来很晦涩,但让我们考虑一下...首先在类别表上进行连接,其中一个特定类别(首先来自类别实例)应该代表具有最小粒度的类别。例如:您的“本地新闻”、“可见”和“前 10”类别。“本地新闻”可能包含最多的条目,而“可见”和“前 10”的条目甚至更少……其中的记录数量甚至最少。使用 THIS 类别作为 where 子句。
因此,假设您有 100,000 篇文章,其中 90,000 篇属于本地新闻,45,000 篇属于 Visible,12,000 篇属于前 10 篇。通过仅对 12,000 篇中的文章开始查询,您将消除大部分数据。
然后加入文章表,并根据其他条件分别作为别名 C2 和 C3 类别,如果找到,则完成,如果没有,则将其排除。
我再次想知道性能影响。我还在类别表上有一个复合索引(article_id,category_id)
In addition to the commonly used IN() and using a HAVING count, I would be interested in the performance difference by doing a multiple-join as follows...
Yes, this may look obscure, but lets think about it... by doing a join FIRST on the categories table where ONE of your specific categories -- THIS FIRST FROM instance of categories should be representative of whichever category would have the smallest granularity. Ex: Your categories of Local News, Visible and First 10. Local news would probably have the most entries, while Visible and First 10 would have even less... of those, which would have even the smallest number of records. Use THIS category as the where clause.
So, say you have 100,000 articles, and 90,000 are in local news, 45,000 in Visible, and 12,000 in First 10. By starting your query on only those in the 12,000, you are eliminating most of the data.
By then joining to the articles table, and categories AGAIN as alias C2 and C3 respectively based on the other conditions, if found, done, if not, its excluded.
Again, I'm wondering the performance impact. I would also have a compound index on the categories table on both (article_id, category_id)
该值不能同时为所有三个值,因此您最好在
WHERE
中使用IN
子句来定义要返回的值。如果您已经在那里有了连接条件,您也希望将其移至ON
子句;即:当然,您可以进入下一步并执行以下操作:
如果您只想显示所有三个类别中出现的文章,那么您可以采取如下方法:
The value cannot be all three values simultaneously, so you'd better use an
IN
clause in yourWHERE
to define which you want to return. Give you've already got a join condition there, you'd want to move that to anON
clause instead as well; ie:Of course, you can go to the next step and do:
If instead you wanted to show only articles that appear in all three categories, then you could take an approach like: