简体   繁体   中英

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.
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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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