I list comments for an entry and I allow for user's to edit existing comments and it keeps track of those revisions.
Table structure:
comments: id, summary
revisions: comment_id, revision_id, timestamp
revisions.comment_id = comments.id
revisions.revision_id = the id of the new comment, further explanation: when they select the existing comment, it will display an edit form and they may enter a new comment and submit it. It will insert it into the comments table as a new comment, grab the last id from that table and set it as revision_id in the revisions table.
I want to do a select distinct on the revisions table (to retrieve multiple comment_id, revision_id into an array to be used within the application):
for example:
select distinct comment_id from revisions
But is it possible to select the appropriate record based on the most recent comment (revisions.timestamp)?
in theory:
select distinct comment_id from revisions WHERE timestamp IS THE LARGEST
revisions table example:
comment_id revision_id timestamp
2 12 20120222180000
2 13 20120222170000
5 18 20120222190000
5 19 20120222200000
In this example of 4 rows, I'd want the query to return two rows,
that being:
comment_id = 2, revision_id = 12 timestamp = 20120222180000
and
comment_id = 5, revision_id = 19 timestamp = 20120222200000
UPDATE: this seems to do the trick, but please let me know if there is a better way
SELECT
distinct comment_id, max(timestamp)
FROM
revisions
GROUP BY
comment_id
UPDATE: I also need to include revision_id, the above query only includes comment_id and timestamp
This did it:
SELECT
distinct a.comment_id as comment_id,
a.revision_id revision_id,
a.timestamp as timestamp
FROM
REVISIONS a
WHERE
a.timestamp = (
SELECT
max(b.timestamp)
FROM
revisions b
WHERE
b.comment_id = a.comment_id
)