I want to ensure this query is correct. I've read the documentation and I believe it is, but I'm looking for the help of some MySql veterans to confirm.
When this query runs, it should return the list of Users created between yesterday and a week before yesterday.
SELECT *
FROM User WHERE DateCreated BETWEEN
((NOW() -INTERVAL 1 DAY) - INTERVAL 7 DAY) AND
(NOW() -INTERVAL 1 DAY);
Thanks for any help.
Related
I need to get data within a range which starts a hour ago. As an example if now time is 08.00 AM, I need to get all the data within 07.00 AM to last 10 days. I have tried with datesub function as below, But seems this is wrong because the date(now()) and date(now()-interval 1 hour) are both same.. Can someone show me how to get this?
.....and date(time)>date(date_sub((now()-interval 1 hour), interval 10 day)) group by ds.....
Assuming time field in your query is either timestamp or datetime.
... and time between cast(concat(date_sub(curdate(), INTERVAL 10 day), ' 00:00:00') as datetime) and DATE_SUB(now(), INTERVAL 1 hour)
Hope this will help.
I'm amending a current query which I run on a fairly regular basis for a membership team looking at recent expiries. The clause in that query is:
and date_expiry between '2019-11-01' and '2019-12-31'
The dates are expanded to cover a 2 month period.
What I'd like to do is to create this query as an excel view in which they can refresh as an when they want.
What I have so far and works to a degree* is the following:
and date_expiry between curdate()- interval 1 month and curdate()+ interval 3 month
However the issue many may have picked up on is that the above query gathers data from today 1 month previous (10/11/2019) and 3 months from today (10/02/2020).
So I've been searching around and the closest I've got was this:
and month(date_expiry) = month(current_date- interval 1 month ) and year(date_expiry)= year(curdate())
This works perfectly for collecting everything in the previous month (01/11/2019-31/11/2019) but I somehow need to add something similar to gather data data for the advanced months.
Help please!
The curdate() suggests MySQL. You can handle full dates as:
where date_expiry >= (curdate() - interval (1 - day(curdate())) day) - interval 1 month and
date_expiry < (curdate() - interval (1 - day(curdate())) day) + interval 1 month
This is convenient because it is index-friendly.
Try DATE_SUB for substraction and DATE_ADD for 2 months advanced
cek this query is this the day you want to cek?
you can change the interval if you want and change the NOW() with your custom date yourself.
to learn about interval check this link
SELECT DATE_SUB(DATE(CONCAT_WS('-', YEAR(NOW()) , MONTH(NOW()), 31)),INTERVAL 1 MONTH) AS lastdaymonthbefore,
DATE_ADD(DATE(NOW()),INTERVAL 2 MONTH) AS 2monthAdvanceFromToday
so you can edit your query like this
AND date_expiry BETWEEN DATE_SUB(DATE(CONCAT_WS('-', YEAR(NOW()) , MONTH(NOW()), 31)),INTERVAL 1 MONTH) AND DATE_ADD(DATE(NOW()),INTERVAL 2 MONTH)
I'm setting up a cleanup on accounts in my database, and only want to run it on accounts that haven't been checked for cleaning in the last 3 months.
Once an account has been checked, regardless if it gets cleaned, I save the date that it's been checked in the following format eg 2019-05-29
When checking for accounts that need to be cleaned, how do I write the
WHERE `accountLastCheckedDate` > 3 months
part of the query please?
Thank you for your time and help.
You want accountLastCheckedDate older than today minus 3 months
SELECT * FROM tablename
WHERE accountLastCheckedDate < DATE_ADD(CURDATE(), INTERVAL -3 MONTH)
or newer?
WHERE accountLastCheckedDate >= DATE_ADD(CURDATE(), INTERVAL -3 MONTH)
This will help you, gl hf
Select * FROM My Table where columname>=DATEADD(m, -3, GETDATE())
If you are interested in date and time:
WHERE `accountLastCheckedDate < `DATE_SUB(NOW(), INTERVAL 3 MONTH);
If you are interested in just the date:
WHERE `accountLastCheckedDate` < DATE_SUB(CURDATE(), INTERVAL 3 MONTH);
I am running a MySQL query to get back to the results in the last 14 days. When I run the following query it keeps returning empty. I am not sure what am I doing wrong here
Below is the query I am running
SELECT * FROM checkout_page WHERE checkout_date = DATE_SUB(NOW(), INTERVAL 14 DAY);
Any help would be really appreciated!
You are trying to get a record which checkout_date is exactly 14 days before current time which is very unlikely to happen thus you will get empty result most of the time. What you want is records happens after that, thus instead of =, change to >=
SELECT * FROM checkout_page WHERE checkout_date >= DATE_SUB(NOW(), INTERVAL 14 DAY);
Thanks for your answer. However, I found out this following query worked
SELECT * FROM checkout_page WHERE DATE(checkout_date) = CURDATE() - INTERVAL 14 DAY ;
I am trying to run a query and get all the records where today is 6 months after the start date.
Here is what I have but it doesn't seem to be working. What am I missing?
select * from users where DATE_ADD(start_date, INTERVAL 1 MONTH) = CURDATE()
Thanks!
This is too long for a comment. First, your code is checking for one month rather than 6 months.
The first possibility is that you don't have any records on that date.
Another is that the dates have times associated with them. If so, you can do:
where DATE_ADD(DATE(start_date), INTERVAL 6 MONTH) = CURDATE()