简体   繁体   中英

Query to select the most recent time in database

Guys I have this php code to return the current time:

$time = date('m/d/Y h:i:s a', time());

I saw this time to my database using this code :

$query = "INSERT into `time` VALUES('', '$time')";

And the time row values update every transaction.

The question here is how I could return the most recent time?

I think i need an sql statement that return the time descendingly but I don't know what is it, so any help?

You can do a simple select, depending on what you want to do:

SELECT * FROM time ORDER BY <time> DESC LIMIT 1

I don't know the name of the column so I wrote <time> .

You just need to do an ORDER BY and a LIMIT 1 :

SELECT  `yourTimeColumn`
FROM    `time`
ORDER BY `yourTimeColumn` DESC
LIMIT 1;

In addition, you could do your INSERT with the current time by just doing:

INSERT INTO `time` VALUES ('', NOW())

if your date is not in datetime format then It needs to be converted to datetime first. Hope the following will help you.

SELECT * FROM time ORDER BY CONVERT(datetime, column) DESC LIMIT 1

column is the name of the time column.

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