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
Related
I have the following mysql query which shows the each day's total cash sale for the current week.
SELECT
sum(Price) as totalprice,
WEEKDAY(CreatedOn) as dayno,
DATE(CreatedOn) as CreatedOn,
AgentID
FROM records
WHERE CreatedOn BETWEEN (CURDATE()-WEEKDAY(CURDATE())) AND CURDATE()
GROUP BY DATE(CreatedOn)
When I run the query it looks like this:
There are records on 30th November(today's date). So,
day 0 (Monday) no cash sale
day 1 (Tuesday) $5049
day 2 (Wednsday) $99
Nothing is displayed for day 3 (Thursday/today). I cannot figure out the reason there are definitely record in the database but cannot get them to be displayed. I would appreciate any help.
CURDATE() is today's date but at 00:00:00+0000000
"push up" the higher date by 1 day and avoid using between for date/time ranges:
WHERE CreatedOn >= date_sub(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)
AND CreatedOn < date_add(CURDATE(), INTERVAL 1 DAY)
select date_sub(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)
, date_add(CURDATE(), INTERVAL 1 DAY)
The condition in the query currently specifies on or before midnight today, so any rows for today after midnight are going to be excluded.
I think you are intending to specify CreatedOn before midnight of the following day.
I also suggest you don't subtract an integer value from a date/datetime, and instead use the INTERVAL syntax
WHERE CreatedOn >= CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY
AND CreatedOn < CURDATE() + INTERVAL 1 DAY
To test those expressions before we include them in a query, we can run a SELECT statement:
SELECT CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY
, CURDATE() + INTERVAL 1 DAY
and verify that those expressions are returning what we expect, the values we want to use. For testing, we can replace CURDATE() with a date value to test the return for days other than today.
I have a date column in my database. I use SELECT COUNT to calculate the rows between today and 15 days ago:
SELECT count(date) as date
FROM `inv`
WHERE user_id='2'
AND date BETWEEN CURDATE() - INTERVAL 15 DAY
AND CURDATE()
This SQL statement is working. But now I want use SELECT COUNT to calculate the rows between today(-15 days) and 30 days ago. But I am getting an error when I try the following statement:
SELECT count(date) as date
FROM `inv`
WHERE user_id='2'
AND date BETWEEN date(CURDATE(),INTERVAL -15 day)
AND date(CURDATE(),INTERVAL -30 day)
Also I want to know how I can SELECT the rows where the date is more than 30 days ago. Can someone help me with this?
You can use the below to get rows between 15 to 30 days old.
SELECT count(date) as date
FROM `inv`
WHERE user_id=2
AND date BETWEEN CURDATE() - INTERVAL 30 DAY
AND CURDATE() - INTERVAL 15 DAY
Similarly you can use below to get rows that are older than 30 days.
SELECT count(date) as date
FROM `inv`
WHERE user_id=2
AND date < CURDATE() - INTERVAL 30 DAY
Try This
SELECT * FROM "table name" WHERE "user_id=2"
BETWEEN CURDATE() - INTERVAL 30 DAY
AND CURDATE() - INTERVAL 15 DAY
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;
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')
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