我有一个带有列和数据的表“ articles”:
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
我想选择类别所属的行.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需要另一种语法。有人可以帮忙吗?谢谢
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
我为表使用了一些不同的名称,因为在您的示例中,category和之间的区别categories很难注意到。
category
categories