I'm looking for help with a single SELECT statement. My answer/example using two statements:
1st: 40 days from now out to 365 days from now
2nd: NOW out to 40 days
equals all 365 days with a start date 40 days from now
select statement 1:
SELECT id FROM sales
WHERE (saledate BETWEEN DATE_ADD(NOW(), INTERVAL 40 DAY) AND DATE_ADD(NOW(), INTERVAL 365 DAY))
ORDER BY saledate;
select statement 2:
SELECT id FROM sales
WHERE (saledate BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 40 DAY))
ORDER BY saledate;
Related
I need to get a table of product that will expire in the next month.
Should I use this query:
where datediff(expiry_date, now ()) <= 30
Or this query:
expiry_date <= date_add(now(), Interval 1 month)
In first query you are taking month days difference .sometime months may be have 28 days ,30 days or 31 days.sometime query may fail or dead lock occur.
Try Second Query ...
expiry_date <= date_add(now(), Interval 1 month)
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
My current query is:
SELECT COUNT(*) FROM Previous_Appointment WHEREapDateBETWEEN '2017-04-03' AND '2017-04-27'
I need this to run however to just check today's date -7 days, -30 days, etc...
So that it returns the number of instances that have occured.
Try this,
Between today and today -7
SELECT COUNT(*) FROM Previous_Appointment WHERE DATE(apDate) > (NOW() - INTERVAL 7 DAY)
Between today and today -30
SELECT COUNT(*) FROM Previous_Appointment WHERE DATE(apDate) > (NOW() - INTERVAL 30 DAY)
you can use dateadd -7 days
dateadd(day,-7,cast(getdate() as date))
To get count in last week:
select count(*)
from previous_appointment
where apDate between curdate() - interval 7 day and curdate()
To get count in last 30 days:
select count(*)
from previous_appointment
where apDate between curdate() - interval 30 day and curdate()
I need a sql query to find the data from a employee table.
Startdate is present in employee table.
I need to add 90 days in startdate and then I need to check if the startdate lies in the current month or not.
I did try below query:
SELECT *
FROM `employees`
WHERE DATE_ADD(str_to_date(startdate, '%m/%d/%Y'),INTERVAL 90 DAY) BETWEEN '09/01/2016' AND '09/30/2016'
but its not giving me the expected results.(I do have data which should show up if the query is correct.)
Hi i did change the query and ran, please see the result. I am getting results of next month too :(
This is the query
SELECT id,startdate from employees WHERE str_to_date(startdate, '%m/%d/%Y') between DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 90 day) -- start of this month - 90 days and DATE_SUB(LAST_DAY(NOW()), INTERVAL 90 day)
Query output
Rephrased my query...
Where the event was within the dates 90 days before the start and end of this month
WHERE str_to_date(startdate, '%m/%d/%Y') between
DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 90 day) -- start of this month - 90 days
and DATE_SUB(LAST_DAY(NOW()), INTERVAL 90 day) -- End of this month - 90 days
or, for three months
WHERE str_to_date(startdate, '%m/%d/%Y') between
DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-01'), INTERVAL 3 month) -- start of this month - 3 months
and DATE_SUB(LAST_DAY(NOW()), INTERVAL 3 month) -- End of this month - 3 months
I am trying to select data from two periods (Last 30 days and Previous 30 days)
So two 30 day periods.
I have the last 30 day period down:
SELECT ProductID, ProductIDintarget,date_format(Date,'%m/%d/%Y'),SUM(Rev)
FROM datatable
WHERE Date BETWEEN CURDATE()-1 - INTERVAL 31 DAY AND CURDATE()
GROUP BY ProductIDintarget
That is working fine, but now I'm struggling to get the previous 30 day period.
I have tried changing the WHERE statement as:
WHERE Date BETWEEN CURDATE()-32 - INTERVAL 31 DAY AND CURDATE()-32
but no luck..
You can use DATE_ADD() or DATE_SUB() like this:
SELECT
ProductID,
ProductIDintarget,
DATE_FORMAT(Date,'%m/%d/%Y'),
SUM(Rev)
FROM datatable
WHERE `Date` < (DATE_ADD(CURDATE(), INTERVAL 1 MONTH))
AND `Date` > (DATE_SUB(CURDATE(), INTERVAL 2 MONTH))
GROUP BY
ProductID,
ProductIDintarget,
`Date`
Note:- You should backticks if you have keywords as column names.