Skip to content
Advertisement

MySQL self join to find only the next revision

Actually I’m working with WordPress. I want to create a self-join or something similar to find a revision of a post, and the following revision of the same post.

https://www.db-fiddle.com/f/eHnwYABYrVVQAhn8xLJ77q/1

The previous query doesn’t work since it takes, for every record of a, all the revision which have been made and not just the next one.

This is what I get, and I’ve striked out the lines I don’t want. I need only father-child rows.

enter image description here

How can I take just one element?

Advertisement

Answer

For this sample data you need the maximum a.revision_id for each combination of a.post_id, b.revision_id:

Also the condition a.revision_id != b.revision_id is not necessary beacause you already have a.revision_id < b.revision_id.
See the demo.
Results:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement