简体   繁体   中英

SQL Items within the Last Day

In my code, I am trying to find items in an activities table that are within the last day. This query is not returning any results, are there any problems with it? Is there a better query?

$curday = time() - (24*3600);
$query = "SELECT * FROM activities WHERE userid = '$userid' AND  'timestamp' > '$curday'";

There are two choices here, you can get and format the date through PHP or use SQL language to do it. I prefer to do it within the SQL, it also allows me to use the same query in a MySQL client.

This question is essentially the same thing: MySQL SELECT last few days?

This would be the new query: $query = "SELECT * FROM activities WHERE userid = '$userid' AND 'timestamp' > DATE_ADD(CURDATE(), INTERVAL -1 DAY)";

you can try with unix function 'mktime' to get value of yesterday .. as

$curday = mktime(0,0,0,date("m"),date("d")-1,date("Y"));

for reference

if your database will mysql only then you can extract yesterday in sql itself..

SELECT * FROM activities 
WHERE userid = '$userid' 
AND  timestamp >  DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 1 DAY)

one more thing if timestamp is your column name don't put this column inside single quote ..

What you can use is DATE_SUB. This can be used as follows

SELECT * FROM activities 
WHERE userid = '$userid' 
AND  timestamp > date_sub(current_date, interval 1 day)

This way you don't need to work with current date in PHP

如果列是DATE类型,那么在Informix中它将是(TODAY - 1)

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