select all records created within the hour - mysql

startTimestamp < date_sub(curdate(), interval 1 hour)
Will the (sub)query above return all records created within the hour? If not will someone please show me a correct one? The complete query may look as follows:
select * from table where startTimestamp < date_sub(curdate(), interval 1 hour);

Rather than CURDATE(), use NOW() and use >= rather than < since you want timestamps to be greater than the timestamp from one hour ago. CURDATE() returns only the date portion, where NOW() returns both date and time.
startTimestamp >= date_sub(NOW(), interval 1 hour)
For example, in my timezone it is 12:28
SELECT NOW(), date_sub(NOW(), interval 1 hour);
2011-09-13 12:28:53 2011-09-13 11:28:53
All together, what you need is:
select * from table where startTimestamp >= date_sub(NOW(), interval 1 hour);

Related

I Want to Show only The Dates from a MySQL Database Between Today and the Next 30 Days

I have the following Code:
SELECT *
FROM `test_establishments`
WHERE expiry_date > CURDATE() AND expiry_date < CURDATE()+30
The Query returns 0 Rows and I do have dates in the database that expire within the next 30 days. I am not sure what the problem is with the code.
You can use :
select *
from `test_establishments`
where expiry_date between curdate() and curdate() + interval 30 day
Demo
You cannot use + on a datetime. You need to use DATE_ADD instead.
DATE_ADD(CURDATE(), interval 30 day)
Use this code 1 month or 30 days.
WHERE expiry_date > NOW() - INTERVAL 30 DAY
AND expiry_date < NOW() + INTERVAL 30 DAY

How to select rows that is >= 1 DAY ago?

A user can get at max 5 reports before i ban him for 1 day from the chat, when row from this user reach 5 reports i store a date on the row, and i can unban him after 1 day with the sql data < DATE_SUB(NOW(), INTERVAL 1 DAY).
But this way he is not able to be "unbanned" from the chat, if he does not loggin on their account until 47:99 hours, because the INTERVAL 1 DAY will not work. So how can i make a WHERE clause to match a row that is at least 1 DAY ago or that is >= 1 DAY ago? It's possible?
$link_id = $_SESSION['linkN'];
$deletReport = $conn->prepare("DELETE FROM `report_public_chat` WHERE `link_id` = :link_id AND `data` < DATE_SUB(NOW(), INTERVAL 1 DAY)");
$deletReport->bindParam(":link_id", $link_id, PDO::PARAM_INT);
$deletReport->execute();
Use delete condition like
WHERE date >= now() - INTERVAL 1 DAY;
OR
WHERE timediff(now(), my_datetime_field) < '24:00:00'
You can try below - using DATE_SUB(NOW(), INTERVAL 24 hours)
DELETE FROM `report_public_chat` WHERE `link_id` = :link_id AND `data` >= DATE_SUB(NOW(), INTERVAL 24 hours) and `data`<NOW()

MySQL How to select date field exactly 7 days before today and for time last 1 hour

I have a query that selects records created from 1 hour in past from current time.
select ts from <table_name> where ts >= DATE_SUB(NOW(), interval 1 hour);
I can also select date before 7 days using
select count(*) from <table_name> where ts >= DATE_SUB(NOW(), interval 7 day);
How can I use these two date features to get records before 7 days from today and time 1 hour in past from current time.
For example, if the present time is 2015-11-06 10:03:00 then how can I get data for time between 2015-10-30 09:03:00 to 2015-10-30 10:03:00
I tried something like this, but it gives syntax error:
select ts from <table_name> where ts >= DATE_SUB(DATE(NOW()), INTERVAL 7 DAY), interval 1 hour)
select ts from <table_name> where ts >= DATE_SUB(NOW(), INTERVAL 7 DAY), interval 1 hour)
Your examples have syntax errors (too many closing parentheses )). If you want to use DATE_SUB(), you need to use it twice. To get entries between one time and another, use WHERE ... BETWEEN ... AND ...
You can use this:
SELECT ts
FROM iv_split_skill_metrics
WHERE ts BETWEEN
DATE_SUB(
DATE_SUB(DATE(NOW()), INTERVAL 7 DAY),
interval 1 hour)
AND
DATE_SUB(DATE(NOW()), INTERVAL 7 DAY)
Or, even better, skip DATE_SUB() entirely and just do subtraction, like this:
SELECT ts
FROM iv_split_skill_metrics
WHERE ts BETWEEN NOW() - INTERVAL 7 DAY - INTERVAL 1 HOUR
AND NOW() - INTERVAL 7 DAY
Edit: For some reason, you edited your question after I posted this and replaced iv_split_skill_metrics with <table_name> in your question, but the examples above will work regardless. Just use the correct table and column names, of course!
Edit 2: I see now that you want entries between 7 days plus 1 hour ago and 7 days ago. I have tweaked my answer to show you how to do that.
Your goal is not 100% clear but just my attempt:
SELECT ts
FROM table_name
WHERE ts >= DATE_ADD(DATE_ADD(NOW(), INTERVAL -7 DAY), INTERVAL -1 HOUR)
AND ts <= DATE_ADD(NOW(), INTERVAL -7 DAY);
but form performance perspective this query would be much faster:
http://sqlfiddle.com/#!9/9edd1/2
SET #end = DATE_ADD(NOW(), INTERVAL -7 DAY);
SET #start = DATE_ADD(#end, INTERVAL -1 HOUR);
SELECT ts
FROM table_name
WHERE ts BETWEEN #start AND #end;

conditional date query in mysql

I'm trying to structure a query which returns data which if priority = emergency is in the last 2 weeks, and if urgent is in the last month.
WHERE ((priority = 'emergency' and date > DATE_SUB(NOW(), INTERVAL 14 DAY)) or
priority = 'urgent' and date > DATE_SUB(NOW(), INTERVAL 30 DAY))
I know this isn't right, but I'm not sure how to do it.
It just looks like your parentheses are in the wrong place:
WHERE ( priority = 'emergency' and date > DATE_SUB(NOW(), INTERVAL 14 DAY) )
OR ( priority = 'urgent' and date > DATE_SUB(NOW(), INTERVAL 30 DAY) )
Since not all months are 30 days you might have to adjust the interval if you want strictly one month back.

comparing dates by month and year in mysql

I have a table containing data about events and festivals with following columns recording their start and end dates.
Start_Date
End_Date
date format is in YYYY-MM-DD. I need to fetch event details with the following condition.
Need to fetch all events which start with a current month and there end dates can be anything say currentDate+next30days.
I am clear about end date concept. but not sure how I can fetch data whose start dates are in a current month.
For this, I need to compare current year and current month against the Start_Date column in my database.
Can anyone help me to point out as how I can do that?
select * from your_table
where year(Start_Date) = year(curdate())
and month(Start_Date) = month(curdate())
and end_date <= curdate() + interval 30 day
I don't like either of the other two answers, because they do not let the optimizer use an index on start_date. For that, the functions need to be on the current date side.
So, I would go for:
where start_date >= date_add(curdate(), interval 1 - day(curdate()) day) and
start_date < date_add(date_add(curdate(), interval 1 - day(curdate()) day), interval 1 month)
All the date functions are on curdate(), which does not affect the ability of MySQL to use an index in this case.
You can also include the condition on end_date:
where (start_date >= date_add(curdate(), interval 1 - day(curdate()) day) and
start_date < date_add(date_add(curdate(), interval 1 - day(curdate()) day), interval 1 month)
) and
end_date <= date_add(curdate(), interval 30 day)
This can still take advantage of an index.
DateTime functions are your friends:
SELECT
*
FROM
`event`
WHERE
(MONTH(NOW()) = MONTH(`Start_Date`))
AND
(`End_Date` <= (NOW() + INTERVAL 30 DAY))
AND
(YEAR(NOW()) = YEAR(`Start_Date`))
Comparing the year and month separately feels messy. I like to contain it in one line. I doubt it will make a noticeable difference in performance, so its purely personal preference.
select * from your_table
where LAST_DAY(Start_Date) = LAST_DAY(curdate())
and end_date <= curdate() + interval 30 day
So all I'm doing is using the last_day function to check the last day of the month of each date and then comparing this common denominator. You could also use
where DATE_FORMAT(Start_Date ,'%Y-%m-01') = DATE_FORMAT(curdate(),'%Y-%m-01')