Show events from the last 7 days via MySQL - mysql

I am trying to just return all rows for an event that has closed within 7 days of the current date.
My end_date has a format such as 2014-06-25 (Y-m-d), what is the best way to select events between NOW and 7 days ago in the past.
I have the following.. but this isn't correct
SELECT *
FROM end_date
WHERE end_date <= NOW() AND end_date >= DATE_SUB(end_date, INTERVAL 7 DAY)
For instance...
e.g If to day i'd want to say events between 2014-07-14 and 2014-07-07

Try using DATE_ADD
SELECT *
FROM end_date
WHERE end_date <= NOW() AND end_date >= DATE_ADD(now(),INTERVAL -7 day)

You can do something like below:
SELECT .... FROM .... WHERE DATEDIFF(NOW(), end_at) <= 7;

I would suggest figuring out a way to get the current date - 7 days in the scripting format you are wanting to use it and then do a query like this:
SELECT * FROM end_date WHERE end_date >= $calculateddate;

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)

how to calculate next 30 days from current date (MySQL)?

i want to get next 30 days record from current date.
any suggestion or tip will be appreciated.
SELECT end_date FROM master_data
WHERE end_date BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY) AND CURRENT_DATE();
You need DATE_ADD not DATE_SUB, because the next 30 days will be between current date and current date + 30:
SELECT end_date
FROM master_data
WHERE end_date BETWEEN CURRENT_DATE()
AND DATE_ADD(CURRENT_DATE(), INTERVAL 30 DAY) ;
Demo

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

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)

How do I get Timestamp minus 6 weeks in MySQL?

I have a field named timestamp. This is the last time a member was logged in.
I am looking to include a where clause in a query for something like
WHERE timestamp > todays date - 6 weeks
How would I do this?
I am trying to only include users that have logged in in the last 6 weeks.
Thanks
I find this syntax more readable than date_sub, but either way works.
WHERE timestamp >= NOW() - INTERVAL 6 WEEK
If you want to go by "Today" (midnight) instead "now" (current time), you would use this
WHERE timestamp >= DATE(NOW()) - INTERVAL 6 WEEK
where column>=date_sub(now(), interval 6 week)
This link demonstrates how you might acquire a timestamp of yesterday using the format DATE_ADD(CURDATE(), INTERVAL -1 DAY), therefore your query would probably be:
WHERE timestamp > DATE_ADD(CURDATE(), INTERVAL -42 DAY)
You can use between and now():
select somevalue
from yourtable
where yourtimestamp between now() - interval 1 day and now()
for TIMESTAMP there is a TIMESTAMPADD() function
SELECT TIMESTAMPADD(WEEK, -6, CURRENT_TIMESTAMP)
this will return the timestemp of 6 weeks ago
or in the case like the question
SELECT * FROM users
WHERE lastlogin > TIMESTAMPADD(WEEK, -6, CURRENT_TIMESTAMP)
Any luck yet. Have you tried:
>= DATE_SUB(NOW(), INTERVAL 6 WEEK)