Query to select records from a database that were created within the last 24 hours

给你一囗甜甜゛ 提交于 2020-03-06 04:07:26

问题


I am wondering how to query to the database to show results that have been submitted in the last 24 hours.

In the table, topics, I have a collumn set up called start_date which uses timestamps. How would I query the database to find the topics created in the last 24 hours?

Basically, I want to do something like this, but not sure how to use WHERE:

 $query = mysql_query("SELECT * FROM topics WHERE start_date = 'LAST 24 HOURS?'");

Thanks :)


回答1:


You can try with something like this;

$query = mysql_query("SELECT * FROM topics WHERE start_date = DATE_ADD(NOW(), INTERVAL -1 DAY)");

Edited: the math of NOW() - 60*60*24 was wrong. Sorry.




回答2:


Try this:

$query = mysql_query("SELECT * FROM topics WHERE start_date >= ".time() - 24 * 3600);

This is asking your database for all topics with a start date timestamp greater than or equal to the timestamp 24 hours ago. Note that you can use a DATETIME column too, with only a slight modification:

$query = mysql_query("SELECT * FROM topics WHERE start_date >= '".date("Y-m-d H:i:s", time() - 24 * 3600)."'");



回答3:


... where start_date > @date

and your date should be mention to time 1 day before now...

Jimmy's query shall run well too !



来源:https://stackoverflow.com/questions/6052264/query-to-select-records-from-a-database-that-were-created-within-the-last-24-hou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!