user447734
user447734

Reputation: 11

MySQL to return only last date / time record

i have a table that has a serial number, date and time when that serial number was modified. i would like to retrieve the latest time and date when that particular serial number was modified. any suggestions? the dates and times are on different columns.

thanks

Upvotes: 1

Views: 1321

Answers (2)

Arc
Arc

Reputation: 11306

Depends on how one can find the most recent record – can there be multiple rows with the same date and time ?
Is the serial number monotonically increasing or decreasing ?

Try using ORDER BY (DESC sorts from newest to oldest) and LIMIT to get what you want, e.g.

SELECT * FROM `table` ORDER BY `date` DESC, `time`, `serial` DESC LIMIT 1

Upvotes: 0

Jordan Running
Jordan Running

Reputation: 106127

Assuming the data and time columns are of types that MySQL knows how to sort correctly (i.e. DATE and TIME types), this should work:

SELECT * FROM table_name
ORDER BY date_col DESC, time_col DESC
LIMIT 1

Upvotes: 1

Related Questions