简体   繁体   中英

Mysql: Latest items grouped by a string

a question to the mysql-pros:

I have some entrys in the database:

column a | column b | id
------------------------  
"xxxx" | Timestamp | 1
"yyyy" | Timestamp | 2
"xxxx" | Timestamp | 3
"yyyy" | Timestamp | 4

I wanna have the entries with the latest timestamp in column b grouped by the string in column a . So {1,3} and {2,4} are group together (same "xxxx") and if 1 and 4 have the latest timestamp then the result should be {1,4}.

Select id,max(b) from myTable group by a --> just gives me a "random" id with the latest timestamp. Maybe someone here knows a good solution?

tia && regards noircc

Try it using GROUP_CONCAT

SELECT a, GROUP_CONCAT(id) FROM
   ( SELECT * FROM table_name ORDER BY b )
GROUP BY a
LIMIT 1

I'm not sure about timestamp values from your example, but you can order it before group_concat

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