I’m working with a document table in MySQL that looks like this:
| doc_id | version | data |
|--------|---------|------|
| 1 | 1 | ... |
| 2 | 1 | ... |
| 1 | 2 | ... |
| 1 | 3 | ... |
I need to get the most recent version of each document. For the sample data above, I want to fetch these rows:
| doc_id | version | data |
|--------|---------|------|
| 1 | 3 | ... |
| 2 | 1 | ... |
Right now, I’m using a while loop to check and update old versions in my result set. But I’m wondering if there’s a better way to do this directly in SQL. Can anyone help me write a query to get the latest version of each document in one go?