Selecting records from last 24 hours - MySQL - mysql

I'm trying to run this MySQL command to select records with a lead_submitted date in the last 24 hours.
The issue is it grabs future dates too (fyi I'm delaying some leads so basically set a date in the future for these to be processed).
For example, if I run the command:
SELECT id, lead_submitted, processed FROM leads WHERE (lead_submitted > now() - INTERVAL 24 HOUR) ORDER BY id DESC;
I get this result:
10 | 2022-10-04 13:24:13 | N
~
Why is this? I just want to select records from the last 24h.
FTR, when I run SELECT NOW() as now; I get 2022-09-30 14:00:12
Any help would be greatly appreciated.
EDIT:
I've found this works as a workaround, but it feels nasty:
SELECT id, lead_submitted, processed FROM leads WHERE (lead_submitted > now() - INTERVAL 24 HOUR) AND (lead_submitted < now() + INTERVAL 1 SECOND) AND processed = 'N' ORDER BY id DESC LIMIT 1;

You probably want to use:
SELECT id, lead_submitted, processed
FROM leads
WHERE lead_submitted between (now() - INTERVAL 24 HOUR) and now()
ORDER BY id DESC;

Related

Find a record between two dates MySQL

I am attempting to find records in my DB that's end_date is within 24 hours of the current time.
Here is the query I am currently running
select *
FROM record r
WHERE NOW() BETWEEN r.end_date AND DATE_ADD(r.end_date, INTERVAL -1 DAY);
This doesn't bring back any of the records with an end_date within 24 hours, am I using the DATE_ADD function wrong?
I think this is what you need:
select *
FROM record r
where
r.end_date >= date_add(now(), INTERVAL -1 DAY)

Find records from MySQL which is updated within last hour but not created within last hour

I want to query from MySQL where posts will come only which is updated within last hour but not created in last hour.
Here is my MySQL query:
SELECT *
FROM
(SELECT *
FROM posts
WHERE (updated_at >= (NOW() - INTERVAL 30 MINUTE))) AS A
WHERE (created_at < DATE_SUB(NOW(), INTERVAL 60 MINUTE))
But this query doesn't give me right result.
I am attaching sample posts in CSV and adding result output from this query.
Sample: []
Query Output: []
I think you just need both conditions in a WHERE clause:
SELECT *
FROM posts
WHERE
updated_at >= DATE_SUB(NOW(), INTERVAL 1 HOUR) AND
created_at < DATE_SUB(NOW(), INTERVAL 1 HOUR);

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.

Mysql query date between in large table

my query is running about 10 seconds and that's unacceptable.
I am looking for a way to improve this speed but i'm out of options.
I have to find the records between now and 30 days back in a table over 12 million rows.
The following query:
SELECT DATE(DATE) AS FDATE,
SUM(VIEW_COUNT) AS COUNT,
COUNT(IP_ADDRESS) AS CLIENTS
FROM VIEWS
WHERE USERID = 'test'
AND DATE BETWEEN ADDDATE(CURDATE(), INTERVAL -30 DAY) AND CURDATE()
GROUP BY FDATE DESC
I also tried, but the same effect:
SELECT DATE(DATE) AS FDATE,
SUM(VIEW_COUNT) AS COUNT,
COUNT(IP_ADDRESS) AS CLIENTS
FROM VIEWS
WHERE USERID = 'test'
AND DATE >= (DATE(NOW() - INTERVAL 30 DAY) + INTERVAL 0 SECOND)
GROUP BY FDATE DESC
You could try using MySQL Indexes for a better performance in decreasing the query execution time.
http://dev.mysql.com/doc/refman/5.7/en/mysql-indexes.html

Display rows from MySQL where a datetime is within the next hour

I always have trouble with complicated SQL queries.
This is what I have
$query = '
SELECT id,
name,
info,
date_time
FROM acms_events
WHERE date_time = DATE_SUB(NOW(), INTERVAL 1 HOUR)
AND active = 1
ORDER BY date_time ASC
LIMIT 6
';
I want to get up to 6 rows that are upcoming within the hour. Is my query wrong? It does not seem to get events that are upcoming within the next hour when I test it.
What is the correct syntax for this?
I'm going to postulate that you're looking at a group of records that contain a range of DATETIME values, so you probably want something more like this:
SELECT id,
name,
info,
date_time
FROM acms_events
WHERE date_time < DATE_ADD(NOW(), INTERVAL 1 HOUR)
AND date_time >= NOW()
AND active = 1
ORDER BY date_time ASC
LIMIT 6
Otherwise, your query is looking for records with a date_time of exactly "now + 1 hour". I'm assuming all your dates aren't specific to that particular second. ;)
To clarify a bit, DATE_ADD() and DATE_SUB() return exact timestamps, so your query above roughly translates to something like SELECT ... WHERE date_time = '2010-04-14 23:10:05' ORDER BY ..., which I don't think is what you want.
WHERE date_time = DATE_SUB(NOW(), INTERVAL 1 HOUR)
means date_time equals exactly now minus one hour, which would result in any record exactly one hour old.
Why not use
WHERE TIMEDIFF(date_time, NOW()) < '01:00:00'
AND date_time > NOW()