conditional date query in mysql - 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.

Related

MySQL select complete last month

How to select all data from last month (or 30 days)?
I already found some answers, and mostly gives this solution
SELECT *
FROM gigs
WHERE date > DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
ORDER BY date DESC
But this gives me also the dates from the future
I am only interested in the days from last month or 30 days (not next month and beyond)
Is this what you want?
WHERE date > DATE_SUB(CURDATE(), INTERVAL 1 MONTH) AND date <= CURRENT_DATE
I added a condition so the query filters on date not greater than today. I also modified your code so the date range starts one month ago (you had 3 months).
try this code
SELECT * FROM gigs
WHERE date BETWEEN CURDATE() - INTERVAL 30 DAY AND CURDATE()
ORDER BY date DESC
You are asking for two separate things.
The last 30 days is easy.
date between now() - interval 30 day and now()
Data this month is like this:
date between (last_day(Now() - INTERVAL 1 MONTH) + INTERVAL 1 DAY) and last_day(Now())
Data a few months ago is like this:
date between (last_day(Now() - INTERVAL 4 MONTH) + INTERVAL 1 DAY)
and
(last_day(Now() - INTERVAL 3 MONTH) + INTERVAL 1 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;

mysql select dates in 30-day range

This must be simple but I fiddled with it, and didn't get anything I wanted. I have the following code:
SELECT id,title,start_date
FROM events
WHERE start_date > DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND city = '$cityName'
ORDER BY start_date DESC
Now this selects events with dates in this month, but the definition of this month shown in query is different than what I need. I need it to show me events within 30 days and not only this month i.e. august. If I insert an event in august it shows the outcome. If I do insert september, it doesn't even though it is less than 30 days away.
You should change 1 MONTH to 30 DAY:
WHERE start_date > NOW() - INTERVAL 30 DAY
To limit it to 30 days in either direction:
WHERE start_date > NOW() - INTERVAL 30 DAY
AND start_date < NOW() + INTERVAL 30 DAY
How about like this:
...WHERE DATE(start_date) BETWEEN DATE_SUB(NOW(),INTERVAL 30 DAY) and DATE_SUB(NOW(),INTERVAL 1 DAY) AND city...
OR
AND TIMESTAMPDIFF(DAY,YOURDATE,now()) < 30
This gives you a 30 day span
I hope this will help also
SELECT id,title,start_date
FROM events
WHERE city = "$cityName" AND
TIMESTAMPDIFF(DAY,start_date,now()) < 30
ORDER BY start_date DESC

select all records created within the hour

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);