I need to find the rows with the greatest value in their group.
I did a lot of research and I found plenty of solutions: join, temporary table, etc.
Still I wanted to try something with "order by ... having ..." and I found something weird.
This is my query:
SELECT MAX(id) AS MaxValue, MyTable.*
FROM MyTable
GROUP BY name
HAVING id = MaxValue
I get a list of rows as result but I don't have a row for every name.
I obtain many rows when I do this:
SELECT * FROM MyTable WHERE name = 'John'
But the result of the first query doesn't contain any row with the name 'John' and I don't get anything if I do this:
SELECT MAX(id) AS MaxValue, MyTable.*
FROM MyTable
GROUP BY name
HAVING id = MaxValue AND name = 'John'
I don't really understand how it works and it triggers me.
Could anyone explain me this behavior?
I did't ask "how to do that". I just didn't understand how "group by ... having" works.