+----+-----------+---------+------------------------+--------------------------+
| id | articleId | version | title | content |
+----+-----------+---------+------------------------+--------------------------+
| 1 | 1 | 0.0 | ArticleNo.1 title v0.0 | ArticleNo.1 content v0.0 |
| 2 | 1 | 1.0 | ArticleNo.1 title v1.0 | ArticleNo.1 content v1.0 |
| 3 | 1 | 1.5 | ArticleNo.1 title v1.5 | ArticleNo.1 content v1.5 |
| 4 | 1 | 2.0 | ArticleNo.1 title v2.0 | ArticleNo.1 content v2.0 |
| 5 | 2 | 1.0 | ArticleNo.2 title v1.0 | ArticleNo.2 content v1.0 |
| 6 | 2 | 2.0 | ArticleNo.2 title v2.0 | ArticleNo.2 content v2.0 |
+----+-----------+---------+------------------------+--------------------------+
我试图提出一个查询来返回Articles.id,其中Articles.version是最大数字.
实际的文章表包含10,000多个条目.
所以在这个例子中,我只想要返回Articles.id 4和6.
我一直在寻找关键字distinct和函数max(),但似乎无法确定它.
任何建议赞赏……
最佳答案
你需要一个子查询:
原文链接:https://www.f2er.com/mysql/434133.htmlSELECT a.id,a.version
FROM articles a
WHERE a.version = (
SELECT MAX(version)
FROM articles b
WHERE b.articleId = a.articleId
)