我想用下表中的相应 REVID 检索每个 NID 的最后一个“版本”:
+------+-------+-----+---------+----------+
| RHID | REVID | NID | VERSION | ACTION |
+------+-------+-----+---------+----------+
| 1 | 1 | 1 | 1 | CREATE |
+------+-------+-----+---------+----------+
| 2 | 2 | 2 | 1 | CREATE |
+------+-------+-----+---------+----------+
| 3 | 3 | 1 | 2 | UPDATE |
+------+-------+-----+---------+----------+
| 4 | 4 | 1 | 3 | UPDATE |
+------+-------+-----+---------+----------+
| 16 | 3 | 1 | 4 | ROLLBACK |
+------+-------+-----+---------+----------+
当我运行时:
SELECT a.nid, a.revID, MAX(a.version)
FROM `revision_history` a
GROUP BY a.nid;
我希望结果是:
+-----+-------+---------+
| NID | REVID | VERSION |
+-----+-------+---------+
| 1 | 3 | 4 |
+-----+-------+---------+
| 2 | 2 | 1 |
+-----+-------+---------+
相反,结果是:
+-----+-------+---------+
| NID | REVID | VERSION |
+-----+-------+---------+
| 1 | 3 | 4 |
+-----+-------+---------+
| 2 | 2 | 4 |
+-----+-------+---------+
问:获得预期结果的适当查询是什么?
笔记:
这是为了构建一个视图(在 MySQL 中,它不支持子查询)。