Count rows in mysql database where timestamp within X interval - mysql

I am trying to count the rows in a table in my database that were inserted within X hours or X days. I have tried repeatedly but I keep getting empty set responses.
The start_stamp column in my table is formatted like this: 2013-08-07 18:18:37
I have tried many variations on:
select * from mytable where start_stamp = now() interval -1 day;
But all to no avail. Can someone tell me what I am doing wrong?

Rather than selecting rows where start_stamp is equal to now() - 1day, you need rows where it is greater than or equal to that range. Additionally, your syntax is a little off. MySQL's date arithmetic uses column_value - INTERVAL <number> <period>, so you need:
SELECT COUNT(*) AS num_new_rows
FROM mytable
WHERE start_stamp >= NOW() - INTERVAL 1 DAY
Likewise to get n hours ago, use INTERVAL n HOUR
# Within 3 hours...
WHERE start_stamp >= NOW() - INTERVAL 3 HOUR
The syntax for date interval arithmetic is described in a small paragraph beneath the DATE_ADD() function reference in the official MySQL documentation.

Here are a some examples...
All entries within the past 6 hours:
select * from mytable where start_stamp >= now() - interval 6 hour;
All entries within the past 2 days:
select * from mytable where start_stamp >= now() - interval 2 day;
All entries within the past 2 1/2 days:
select * from mytable where start_stamp >= now() - interval 2 day - interval 12 hour;

Related

Get records in mysql database that have dates which will be due in the coming 2 weeks

i want my code to retrieve rows that will be due in the less than 2 weeks so that i can send a reminder to the user to renew there subscription
$Q = "SELECT * FROM records WHERE dateDue BETWEEN (dateDue - INTERVAL 2 WEEK) AND NOW()";
That would be:
select *
from records
where dateDue > now() and dateDue <= now() + interval 2 week
Or, if your date has not time component (that is, you have a date, not a timestamp):
select *
from records
where dateDue > current_date and dateDue <= current_date + interval 2 week
Your question does not mentions whether you want inclusive or exclusive intervals. You might need to adjust the inequalities accordingly.

Get records where timestamp column is from 2 months ago but not from 1 month ago

I am trying to query a MySQL database where a timestamp column called CrashReceived is from two months ago but not including records from 1 month agao.
The idea is what I have a count comparison so I can get the count of records from the last month and then compare the count to the previous month.
I'm therefore testing a query to retrieve all records where the column value is from 2 months ago but doesn't include records from 1 month ago.
Below is the query I have so far:
SELECT * FROM crash_info WHERE ProjectID='14720069' AND (NOW() - INTERVAL 2 MONTH < CrashReceived AND (CrashReceived <> NOW() - INTERVAL 1 MONTH));
But this is returning records that are include today and yesterday so not doing what I'm after.
You can use between operator for this:
SELECT * FROM crash_info
WHERE ProjectID='14720069' AND
CrashReceived BETWEEN (NOW() - INTERVAL 2 MONTH) AND (NOW() - INTERVAL 1 MONTH));
Field must be bigger than time - 2 month but smaller than time - 1 month.
CrashReceived > (DATE_SUB(NOW(), INTERVAL 2 MONTH)
AND CrashReceived <= (DATE_SUB(NOW(), INTERVAL 1 MONTH)
You should apply an index to the field CrashReceived.

SQL Query by created 3 hours ago

Given a set of data with date_created stored like 2017-04-13 23:29:52, how would I construct an SQL query to select all items that were created within the last 3 hours?
I originally thought to do something like this:
SELECT
*,
MAX(date_created)
FROM items
GROUP BY date_created
but that would not be exactly what I want. I'm not sure how to go about this.
Use NOW() and INTERVAL in your WHERE clause
SELECT * FROM items WHERE date_created <= NOW() - INTERVAL 180 minute AND date_created >= NOW() - INTERVAL 210 minute
This one uses CURDATE, CURDATE and DATE_ADD:
SELECT *
FROM items
WHERE DATE(date_created) = CURDATE()
AND TIME(date_created) BETWEEN CURTIME() AND DATE_ADD(CURTIME(), INTERVAL -3 HOUR)
select * from items where
extract(hours from age(current_timestamp, date_created))>=3;
Extract keyword would extract hours difference from current timestamp and return only that is greater than or equal to 3.

Get number of records in table during last day and last week with single query in MySQL 5.5

I have table with timestamp in one column (MySQL 5.5). Is it possible to get number of records during last 1 day (last 86400 seconds) and number of records during last week (last 604800 seconds) in single query?
I know how to do it with 2 queries, but it would be nice to know if there is some neat solution to this.
timestamp > DATE_SUB(NOW(), INTERVAL 1 DAY)
timestamp > DATE_SUB(NOW(), INTERVAL 1 WEEK)
Quick and Dirty is just a Union then.
Select '1 Days', Count(*) as NumberOf from sometable
Where `timestamp` > DATE_SUB(NOW(), INTERVAL 1 DAY)
union
Select '7 Days', Count(*) as from sometable
Where `timestamp` > DATE_SUB(NOW(), INTERVAL 1 WEEK)
Gets painful and perhaps expensive if you have a lot of ranges though. f So you might want to look at DateDiff to calculate the interval once and then count that.

How to do : "Between TODAY AND TODAY-7"?

I need to find the account created for the current day, et for the last 7 days.
To find my results for today, it works, and I do this :
SELECT * FROM `account` where DATE(created_at) = DATE(NOW())
But I don't know how to do to get the last 7days account.
I tried something like this, but without success :
SELECT * FROM `account` where DATE(created_at) BETWEEN DATE(NOW()) AND DATE(NOW()-7)
Have you an idea ?
in mysql:
SELECT * FROM `account`
WHERE DATE(created_at) > (NOW() - INTERVAL 7 DAY)
Try:
BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()
If created_at has an index and you wouldn't like to prevent the optimiser from using it, I would recommend the following pattern (assuming created_at contains both date and time):
WHERE created_at >= CURRENT_DATE - INTERVAL 7 DAY
AND created_at < CURRENT_DATE + INTERVAL 1 DAY
This spans the range from the day exactly one week ago till today (inclusive), so 8 days in total.
also have a look at MySQL functions ADDDATE(), DATE_ADD(), DATE_SUB()
e.g.
ADDDATE(DATE(NOW()), -7)