在MySQL中查找ID最大值的行 [英] Find row with maximum value of id in MySQL
本文介绍了在MySQL中查找ID最大值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看看下面称为"Articles"的MySQL表:
Take a look at the MySQL table below called "Articles":
+----+-----------+---------+------------------------+--------------------------+
| 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是最大数量.
Im trying to come up with a query to return Articles.id where Articles.version is the maximum number.
实际的Articles表包含10,000多个条目.
The actual Articles table contains over 10,000 entries.
因此,在此示例中,我仅希望返回Articles.id 4和6. 我一直在关注关键字distinct和函数max(),但似乎无法理解它.
So in this example I ONLY want Articles.id 4 and 6 to be returned. Ive been looking at keyword distinct and function max() but cant seem to nail it.
任何建议表示赞赏...
Any suggestions appreciated...
推荐答案
您需要在此处进行子查询:
You need a sub query here:
SELECT a.id, a.version
FROM articles a
WHERE a.version = (
SELECT MAX(version)
FROM articles b
WHERE b.articleId = a.articleId
)
这篇关于在MySQL中查找ID最大值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文