Can I use non-aggregate columns with group by?

前端 未结 6 844
不思量自难忘°
不思量自难忘° 2020-12-09 09:24

You cannot (should not) put non-aggregates in the SELECT line of a GROUP BY query.

I would however like access the one of the non-aggregate

6条回答
  •  醉话见心
    2020-12-09 09:53

    You cannot (should not) put non-aggregates in the SELECT line of a GROUP BY query.

    You can, and have to, define what you are grouping by for the aggregate function to return the correct result.

    MySQL (and SQLite) decided in their infinite wisdom that they would go against spec, and allow queries to accept GROUP BY clauses missing columns quoted in the SELECT - it effectively makes these queries not portable.

    It really seems like there should be a way to get this information without needing to join.

    Without access to the analytic/ranking/windowing functions that MySQL doesn't support, the self join to a derived table/inline view is the most portable means of getting the result you desire.

提交回复
热议问题