Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

8
  • 5
    Just one note: The result is not incorrect (it follows MySQL rules), but it is unpredictable, meaning, columns not part of group by and not aggregated (and not functionally dependent) will return a 'random' value from the corresponding group. As the manual states: "the server is free to choose any value from each group"
    – Pred
    Commented Jan 27, 2017 at 6:36
  • 18
    +1 for me. I have no idea why the "accepted" answer above has been accepted. Your way is the correct way and the accepted answer is the usual hacky fix which will lead to more issues in future.
    – Jcov
    Commented Aug 26, 2020 at 10:23
  • 6
    @Jcov ... and I couldn't agree with your comment more +1. Turning off the ANSI restriction on GROUP BY is almost always a bad idea. It scares me that so many users are taking the wrong advice and using it. Commented Aug 26, 2020 at 10:29
  • 3
    @TimBiegeleisen welcome to modern copy+paste development. "I don't need to think, someone who has read a random thing somewhere from an unreliable source has already done the thinking for me".
    – Jcov
    Commented Aug 26, 2020 at 10:33
  • 5
    This should be the accepted answer. It answers the question of the best solution and explains why it doesn't work in the current setup. Not all developers have access to these settings or are allowed to change these settings.
    – Bird87 ZA
    Commented Apr 21, 2021 at 15:27