# MySQL 中 Group By 后如何选择记录

## 误区

SELECT * FROM
(SELECT * FROM douyin_official_video WHERE ORDER BY created_time DESC) t
GROUP BY video_id


If ONLY_FULL_GROUP_BY is disabled, a MySQL extension to the standard SQL use of GROUP BY permits the select list, HAVING condition, or ORDER BY list to refer to nonaggregated columns even if the columns are not functionally dependent on GROUP BY columns. This causes MySQL to accept the preceding query. In this case, the server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic, which is probably not what you want. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause.

ONLY_FULL_GROUP_BY 这个配置决定了能否在 SELECT 后的字段中出现 Group By 后没有的字段。ONLY_FULL_GROUP_BY 为 disabled时，允许SELECT 后的字段中出现 Group By 后没有的字段；ONLY_FULL_GROUP_BY 为 enable 时， 只能 SELECT 在 Group By 后出现的字段。而在大多数情况下，为了减轻程序员编写sql语句的压力，ONLY_FULL_GROUP_BY 都会建议设为 disabled。

## 解决方案

SELECT * FROM douyin_official_video WHERE id IN
(SELECT MAX(id) FROM douyin_official_video GROUP BY video_id) t


## 结论

SELECT * FROM table GROUP BY


SELECT * FROM (SELECT * FROM table ORDER BY ) GROUP BY