Using ORDER BY and GROUP BY together

前端 未结 7 1965
清酒与你
清酒与你 2020-11-28 05:30

My table looks like this (and I\'m using MySQL):

m_id | v_id | timestamp
------------------------
6    |   1  | 1333635317
34   |   1  | 1333635323
34   |            


        
7条回答
  •  一个人的身影
    2020-11-28 05:46

    If you really don't care about which timestamp you'll get and your v_id is always the same for a given m_i you can do the following:

    select m_id, v_id, max(timestamp) from table
    group by m_id, v_id
    order by timestamp desc
    

    Now, if the v_id changes for a given m_id then you should do the following

    select t1.* from table t1
    left join table t2 on t1.m_id = t2.m_id and t1.timestamp < t2.timestamp
    where t2.timestamp is null
    order by t1.timestamp desc
    

提交回复
热议问题