mySQL SELECT FROM 表 WHERE ... AND ... AND ... AND

发布于 2024-12-14 11:27:32 字数 1010 浏览 2 评论 0原文

我有一个包含列和数据的表“文章”:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青衫儰鉨ミ守葔 2024-12-21 11:27:33
SELECT 
   Articles.article_id, 
   COUNT( Categories.article_id ) AS total 
FROM CategoryArticles 
LEFT JOIN Articles USING (article_id)
WHERE 
   CategoryArticles.category_id IN (1,2,3)
GROUP BY CategoryArticles.article_id
HAVING  total = 3

我对表使用了一些不同的名称,因为在您的示例中,很难注意到 categorycategories 之间的区别。

SELECT 
   Articles.article_id, 
   COUNT( Categories.article_id ) AS total 
FROM CategoryArticles 
LEFT JOIN Articles USING (article_id)
WHERE 
   CategoryArticles.category_id IN (1,2,3)
GROUP BY CategoryArticles.article_id
HAVING  total = 3

I used a bit different names for table because in your example the distinction between category and categories is hard to notice.

開玄 2024-12-21 11:27:33

一行的列不能同时为1、2或3,这是AND规定的。在 WHERE 条件中使用 OR。更好的是 - 为了便于阅读 - 您可以使用 IN

SELECT ...
WHERE `categories`.`article_id` IN(1,2,3)

An column of a row cannot be 1, 2 or 3 at the same time, which is what AND stipulates. Use OR in your WHERE condition. Better yet - for readability - you can use IN:

SELECT ...
WHERE `categories`.`article_id` IN(1,2,3)
A君 2024-12-21 11:27:33

除了常用的 IN() 和使用 HAVING 计数之外,我还对通过执行如下多重连接所带来的性能差异感兴趣...

SELECT STRAIGHT_JOIN
      articles.article_id, 
      articles.title, 
      articles.body
   FROM 
      categories c1
         JOIN articles
            on c1.article_id = articles.article_id
         JOIN categories c2
            on c1.article_id = c2.article_id
           AND c2.category_id = 2
         JOIN categories c3
            on c1.article_id = c3.article_id
           AND c3.category_id = 3
  WHERE
     c1.Category_ID = 1

是的,这可能看起来很晦涩,但让我们考虑一下...首先在类别表上进行连接,其中一个特定类别(首先来自类别实例)应该代表具有最小粒度的类别。例如:您的“本地新闻”、“可见”和“前 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...

SELECT STRAIGHT_JOIN
      articles.article_id, 
      articles.title, 
      articles.body
   FROM 
      categories c1
         JOIN articles
            on c1.article_id = articles.article_id
         JOIN categories c2
            on c1.article_id = c2.article_id
           AND c2.category_id = 2
         JOIN categories c3
            on c1.article_id = c3.article_id
           AND c3.category_id = 3
  WHERE
     c1.Category_ID = 1

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)

丢了幸福的猪 2024-12-21 11:27:33

该值不能同时为所有三个值,因此您最好在 WHERE 中使用 IN 子句来定义要返回的值。如果您已经在那里有了连接条件,您也希望将其移至 ON 子句;即:

SELECT articles.article_id, articles.title, articles.body, categories.article_id, categories.category_id 
FROM articles
INNER JOIN categories ON articles.article_id = categories.article_id 
WHERE categories.article_id IN ( 1, 2, 3 )

当然,您可以进入下一步并执行以下操作:

SELECT articles.article_id, articles.title, articles.body, category.category
FROM articles
INNER JOIN categories ON articles.article_id = categories.article_id 
INNER JOIN category ON categories.category_id = category.category_id
WHERE categories.article_id IN ( 1, 2, 3 )

如果您只想显示所有三个类别中出现的文章,那么您可以采取如下方法:

SELECT articles.article_id, articles.title, articles.body
FROM articles
INNER JOIN categories AS c1
ON articles.article_id = c1.article_id 
AND   c1.category_id = 1
INNER JOIN categories AS c2
ON articles.article_id = c2.article_id 
AND   c2.category_id = 2
INNER JOIN categories AS c3
ON articles.article_id = c3.article_id 
AND   c3.category_id = 3

The value cannot be all three values simultaneously, so you'd better use an IN clause in your WHERE to define which you want to return. Give you've already got a join condition there, you'd want to move that to an ON clause instead as well; ie:

SELECT articles.article_id, articles.title, articles.body, categories.article_id, categories.category_id 
FROM articles
INNER JOIN categories ON articles.article_id = categories.article_id 
WHERE categories.article_id IN ( 1, 2, 3 )

Of course, you can go to the next step and do:

SELECT articles.article_id, articles.title, articles.body, category.category
FROM articles
INNER JOIN categories ON articles.article_id = categories.article_id 
INNER JOIN category ON categories.category_id = category.category_id
WHERE categories.article_id IN ( 1, 2, 3 )

If instead you wanted to show only articles that appear in all three categories, then you could take an approach like:

SELECT articles.article_id, articles.title, articles.body
FROM articles
INNER JOIN categories AS c1
ON articles.article_id = c1.article_id 
AND   c1.category_id = 1
INNER JOIN categories AS c2
ON articles.article_id = c2.article_id 
AND   c2.category_id = 2
INNER JOIN categories AS c3
ON articles.article_id = c3.article_id 
AND   c3.category_id = 3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文