Mysql
 sql >> Database >  >> RDS >> Mysql

Restituisce l'"ultima" riga di ogni "raggruppa per" in MySQL

Prova questa query -

SELECT t1.* FROM foo t1
  JOIN (SELECT uid, MAX(id) id FROM foo GROUP BY uid) t2
    ON t1.id = t2.id AND t1.uid = t2.uid;

Quindi usa EXPLAIN per analizzare le query.

SELECT t1.* FROM foo t1
  LEFT JOIN foo t2
    ON t1.id < t2.id AND t1.uid = t2.uid
WHERE t2.id is NULL;