Do all columns in a SELECT list have to appear in a GROUP BY clause

后端 未结 7 1195
悲&欢浪女
悲&欢浪女 2020-11-30 19:50

My lecturer stated:

All column names in SELECT list must appear in GROUP BY clause unless name is used only in an aggregate function

I\'m ju

相关标签:
7条回答
  • 2020-11-30 20:23

    Actually, in MySQL you don't have to group by all columns. You can just group by whatever columns you want. The problem is, it will just pull a random value (from the set of available rows in the group) for the fields which aren't in the group by. If you know that you are grouping by something that is a unique key, there's no point in grouping by the rest of the fields, as they will already all have the same value anyway. It can actually speed it up to not have to group by every field when it is completely unnecessary.

    0 讨论(0)
提交回复
热议问题