user1650487 user1650487 - 4 years ago 121
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 Source

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 );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download