简体   繁体   English

mysql:非分组项目在使用分组依据时崩溃

[英]mysql: Non-grouped items collapse on using group by

Problem: I am currently grouping a table in mysql by one column and this will collapse other columns to one row for each group. 问题:我目前正在将mysql的表按一列分组,这会将其他列折叠为每一组的一行。
How can I control which row will be displayed after the group by? 如何控制分组依据后将显示哪一行?
For example, for one of the columns I would like to sort it and then have only the top column in each group be displayed. 例如,对于其中一列,我想对其进行排序,然后仅显示每个组中的第一列。

Most cases where this comes up, you don't really want aggregation. 在大多数情况下,您并不需要聚合。 You want to filter the data. 您要过滤数据。 So, say you have a bunch of records for entities that are updated over time. 因此,假设您有一堆随时间更新的entities记录。 If you want the most recent row for each entity, you can use: 如果要每个实体的最新行,可以使用:

select t.*
from t
where t.created_at = (select max(t2.created_at) from t t2 where t2.entity_id = t.entity_id);

However, the correct thing for you to do is probably to delete this question. 但是,您要做的正确的事情可能是删除此问题。 Ask another question with sample data, desired results, and the query that you are using. 使用样本数据,期望的结果和您使用的查询问另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM