user1650487 user1650487 - 4 months ago 7
SQL Question

MySQL Select Latest for multiple values

enter image description here

I have this database and want to query the latest dates for only the ID's 5,10,15,20. The result of this query should be only the first four rows of that database.


How would I do that?

Answer

You can use in and group by

 select * 
 from my_table 
 where (date, myID) in (select max(date), myID 
                       from my_table 
                       where myID in  (5,10,15,20)
                       group by myID );
Comments