MySQL:从每个类别中抓取一行,但删除多个类别中发布的重复行
我有一个文章数据库,这些文章按类别存储。对于我的主页,我想从每个类别中获取一篇文章(我不在乎是哪个类别)。然而,有些文章交叉发布到多个类别,因此它们出现了两次。
我有一个名为 tblReview 的表,其中包含文章字段(reviewID、headline、reviewText)和一个名为 tblWebsiteContent 的表,该表告诉网站文章属于哪个类别(id、reviewID、categoryID),最后还有一个名为 tblCategories(categoryID、categoryName)的表)存储类别。
我的查询基本上连接这些表并使用 GROUP BY tblCategory.categoryID。如果我尝试将“tblReview.reviewID”添加到 GROUP BY 语句中,我最终会得到数百篇文章,而不是 22 篇(我拥有的类别数)。
我感觉这需要一个子查询,但我的测试工作没有起作用(不确定哪个查询需要包含我的连接/字段列表/where 子句等)。
谢谢!
马特
I have a database of articles, which are stored in categories. For my homepage, I want to grab an article from each category (I don't care which). However, some articles are crossposted to multiple categories, so they come up twice.
I have a table called tblReview with the article fields (reviewID, headline, reviewText) and a table called tblWebsiteContent that tells the site which categories the articles are in (id, reviewID, categoryID) and finally, a table called tblCategories (categoryID, categoryName) which stores the categories.
My query basically joins these tables and uses GROUP BY tblCategory.categoryID. If I try adding 'tblReview.reviewID' into the GROUP BY statement, I end up with hundreds of articles, rather than 22 (the number of categories I have).
I have a feeling this needs a subquery but my test efforts haven't worked (not sure which query needs to contain my joins / field list / where clause etc).
Thanks!
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此查询将为每个类别选择与该类别的 Max reviewId 相对应的文章标题(您说“我不在乎哪个”)
this query will select for each category an article headline corresponding to the Max reviewId for that category (you said 'I don't care which')
尝试使用
SELECT DISTINCT
。 (只有当您的SELECT
仅提取文章 ID 时,这才有效。)Try using
SELECT DISTINCT
. (This will only work if yourSELECT
is only pulling the article ID.)选择不同的评论 ID
select DISTINCT reviewID