简体   繁体   中英

Get last value in table depending on column values

I have a table that has 3 columns

id, Name, time

id is an incrementing int value Name is a string identifier time is just a epoc value of when the item was added to the table.

I can have something that looks like this.

1, Jeff, 1520288589
2, Jeff, 1520288590
3, Jeff, 1520288591
4, Tim, 1520288592
5, Jeff, 1520288593

I would like to know how I can create an SQL statement to request the last Tim row and the last Jeff row.

In your case, you can just do:

select max(id), name, max(time)
from t
group by name;

The id and time seem to both be incrementing over time.

A more general solution is:

select t.*
from t
where t.time = (select max(t2.time) from t t2 where t2.name = t.name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM