I have a simple table comments (id INT, revision INT, comment VARCHAR(140))
with some content like this:
1|1|hallo1|
1|2|hallo2|
1|3|hallo3|
2|1|hallo1|
2|2|hallo2|
I'm searching for an SQL statement which will return each comment with the highest revision:
1|3|hallo3|
2|2|hallo2|
I've come up with this solution:
select id, revision, comment
from comments
where revision = (
select max(revision)
from comments as f
where f.id = comments.id
);
but it is very slow on large data sets. Are there any better queries to accomplish this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…