Returning the 'last' row of each 'group by' in MySQL

后端 未结 6 605
既然无缘
既然无缘 2020-11-27 06:34

Is there a more efficient way of doing the following?

select * 
    from foo as a
    where a.id = (select max(id) from foo where uid = a.uid group by uid)
          


        
6条回答
  •  抹茶落季
    2020-11-27 07:21

    Returning the last row of each GROUP BY in MySQL with WHERE clause:

    SELECT *
    FROM foo
    WHERE id IN (
      SELECT Max(id)
      FROM foo
      WHERE value='XYZ'
      GROUP BY u_id
    )
    LIMIT 0,30
    

提交回复
热议问题