I’ve made a small revision system that stores the new version of a text each time it is modified.
The table looks like this:
+-------+-----+----------+
| revID | nID | text |
+-------+-----+----------+
| 1 | 1 | foo |
+-------+-----+----------+
| 2 | 1 | newfoo |
+-------+-----+----------+
| 3 | 2 | bar |
+-------+-----+----------+
| 4 | 2 | baz |
+-------+-----+----------+
| 5 | 3 | a |
+-------+-----+----------+
What SQL statement will give me the last inserted row for every nID? Like this:
+-------+-----+----------+
| revID | nID | text1 |
+-------+-----+----------+
| 2 | 1 | newfoo |
+-------+-----+----------+
| 4 | 2 | baz |
+-------+-----+----------+
| 5 | 3 | a |
+-------+-----+----------+
The idea of creating a subquery is to get the maximum
RevIDfor eachNID, then joining it against the table itself but with two joining condition: that it match withNIDand that it also match withRevID