LMSouq
database Open

SQL select only rows with max value on a column

MA
Majid Fouladpour
1 month ago
3 views
Problem Description
I have this table for documents (simplified version here): | id | rev | content | |------|-------|--------------------------------------| | 1 | 1 | ... | | 2 | 1 | ... | | 1 | 2 | ... | | 1 | 3 | ... | How do I select one row per id and only the greatest rev? With the above data, the result should contain two rows: `[1, 3, ...]` and `[2, 1, ..]`. I'm using ***MySQL***. Currently I use checks in the `while` loop to detect and over-write old revs from the resultset. But is this the only method to achieve the result? Isn't there a **SQL** solution?

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base