简体   繁体   English

从MySQL的修订表中获取最新条目

[英]Getting latest entries from the revision table in mysql

I have a revision database table with multiple revisions for pages(pageId) under a website(webId).I want to write a query to get only the latest revisions of a website.For example the table below shows two files with id 1 and 2,now i want to get the revision number 101 and 104 as they are latest for their respective pages.I am using mysql 我有一个修订数据库表,其中包含网站(webId)下页面(pageId)的多个修订版本。我想编写一个查询以仅获取网站的最新修订版本。例如,下表显示了两个ID为1和2的文件,现在我想获取修订号101和104,因为它们是各自页面的最新版本。我正在使用mysql

+----+---------+-------------+---------------------+-------+---------------------+
| id | userId  | webId       |revisionCont         | pageId| dateUpdated         |
+----+---------+-------------+---------------------+-------+---------------------+
| 100|       1 |           2 | some text           | 1     | 2014-01-07 08:00:00 |
| 101|       1 |           2 | some text           | 1     | 2014-01-07 08:01:00 |
| 103|       2 |           2 | some text           | 2     | 2014-01-07 08:15:32 |
| 104|       2 |           2 | some text           | 2     | 2014-01-07 09:10:32 |
+----+---------+-------------+---------------------+-------+---------------------+

I am not able to figure out how can I do it?Can someone guide me over it? 我不知道该怎么办?有人可以指导我吗?

try this 尝试这个

 SELECT max(id) id FROM revision 
 GROUP BY pageId

DEMO HERE 此处演示

output: 输出:

ID
104
101

EDIT: if you need to filrter or order the output then just add ORDER BY dateUpdated DESC 编辑:如果您需要过滤或订购输出,则只需添加ORDER BY dateUpdated DESC

SELECT MAX(id) id FROM revision GROUP BY pageId

应该做。

To obtain the complete row do: 要获取完整的行,请执行以下操作:

SELECT * FROM revision WHERE Id IN (
  SELECT MAX(id) FROM revision GROUP BY pageId ORDER BY dateUpdated DESC
)

Here is query example: 这是查询示例:

SELECT id FROM revision GROUP BY pageId ORDER BY dateUpdated DESC

It will get all records IDs (101 and 104), for each pageId, ordered by dateUpdated (descended) 它将为每个pageId获取所有记录ID(101和104),按dateUpdated(降序)排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM