nix, shell, perl, php, mysql and mac os x tips and tricks

Thursday, August 28, 2008

MySQL Select the maximum row in each group

SELECT f.id AS id, f.client AS client, f.customer_id AS customer_id, f.bill_email AS bill_email
FROM (
   SELECT client, max(id) AS top_id
   FROM activity GROUP BY client
) AS x INNER JOIN activity AS f ON f.client = x.client and f.id = x.top_id;

No comments:

Post a Comment