Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting rows where a timestamp is less than 24 hours old

For the query below, how could I count the number of rows where datesent is less than 24 hours old? (The field datesent is a timestamp).

Thanks in advance,

John

  $message = "SELECT datesent, recipient                FROM privatemessage                WHERE recipient = '$u'";     $messager = mysql_query($message);  $messagearray = array();  
like image 618
John Avatar asked Dec 27 '10 19:12

John


People also ask

How do I select a record from last 24 hours in SQL Server?

If you want to select the last 24 hours from a datetime field, substitute 'curate()' with 'now()'. This also includes the time.

How do I get last one hour data in SQL?

Here is the SQL to show latest time using now() function. Here is the SQL to get last 1 hour data in MySQL. In the above query, we select only those rows whose order_date falls within past 1 hour interval. We use INTERVAL clause to easily substract 1 hour interval from present time obtained using now() function.

Does timestamp require more storage than datetime?

Size − Datetime requires 5 bytes along with 3 additional bytes for fractional seconds' data storing. On the other hand, timestamp datatype requires 4 bytes along with 3 additional bytes for fractional seconds' data storing.

What is timestamp value?

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision.


2 Answers

Use:

SELECT COUNT(*) AS cnt   FROM PRIVATEMESSAGE pm  WHERE pm.datesent >= DATE_SUB(NOW(), INTERVAL 1 DAY) 
like image 174
OMG Ponies Avatar answered Sep 23 '22 00:09

OMG Ponies


You can use the DATE_SUB function. Subtract one day from the current date in the where clause.

Something like

DATE_SUB(NOW(), INTERVAL 1 DAY) 

EDIT: changed CURTIME() to NOW()

like image 29
Suirtimed Avatar answered Sep 26 '22 00:09

Suirtimed