I want to retrieve the last “VERSION” for each NID with the corresponding REVID in the following table:
+------+-------+-----+---------+----------+
| 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 |
+------+-------+-----+---------+----------+
When I run:
SELECT a.nid, a.revID, MAX(a.version)
FROM `revision_history` a
GROUP BY a.nid;
I expect the result to be:
+-----+-------+---------+
| NID | REVID | VERSION |
+-----+-------+---------+
| 1 | 3 | 4 |
+-----+-------+---------+
| 2 | 2 | 1 |
+-----+-------+---------+
Instead the results are:
+-----+-------+---------+
| NID | REVID | VERSION |
+-----+-------+---------+
| 1 | 3 | 4 |
+-----+-------+---------+
| 2 | 2 | 4 |
+-----+-------+---------+
Q. What is the appropriate query to obtain the results I expect?
NOTE:
This is to build a view (which, in MySQL, does not support subqueries).
try this:
SQL fiddle demo