Query results on every 6 months (display results 7 days before) - mysql

I am trying to make an app for the "car maintenance shop".
In MySQL table I have
ID
CAR_PLATE
MAINTENANCE_DATE (timestamp)
Every car that goes to the maintenance, must come again after every 6 months for a new check.
When the car arrives for the first time, the date will be timestamped in field "maintenance_date".
Question is...
How to display which car (license plate) is (let's say 7 days) near the period of the new check (maintenance)?
example
CAR_PLATE = XYXYXY,
MAINTENANCE_DATE = 1.1.2018
At 25.6 I should get a car plate same as the date (d/m) of the new maintenance (exactly 6 months after) and it would be displayed until maintenance day passes.
P.S.
I tried with "maintenance_date > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 7 DAY)";" but I am not on a good way..

The following code will be helpful for you.
SELECT CAR_PLATE
FROM table_name
WHERE DATE_ADD(MAINTENANCE_DATE, INTERVAL 6 MONTH)
BETWEEN CURDATE() AND (DATE_ADD( CURDATE(), INTERVAL 7 DAY));
If you are taking time for your comparison simply replace NOW() instead of CURDATE().Check the code given below:
SELECT CAR_PLATE
FROM table_name
WHERE DATE_ADD(MAINTENANCE_DATE, INTERVAL 6 MONTH)
BETWEEN NOW() AND (DATE_ADD( NOW(), INTERVAL 7 DAY));
I have attached my SQLfiddle with this. Thank you!

Try the following, simple approach to select records where the difference in days is between 0 and 7 (in the future).
SELECT ID, CAR_PLATE, MAINTENANCE_DATE
FROM <Table>
WHERE TIMESTAMPDIFF(DAY, NOW(), MAINTENANCE_DATE) BETWEEN 0 AND 7
;

Related

How to get last 1 hour to last 10 days data in mysql

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.

Does this query get the past 7 days?

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.

Get records where today is 6 months after date

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

mysql request every week since last 6 months

I'm trying to request my database to get numbers of rows where a specific login appears from last 6 months ordered by weeks.
The output has to be something like this :
My request is like this :
SELECT count(*) FROM table1 where (`EndDate` BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 MONTH) and CURDATE()) AND the_login LIKE 'LOGIN 1';
It shows every rows from last 6 month for now.
Could you complete my request or change it if needed please ?
You can use YEAR() and WEEK() to extract them from the date, and group by them .
Note that I didn't use only WEEK because its not enough, if you'll perform this before June, weeks can be misunderstood.
Also, don't use LIKE for exact match, use them only for partially comparison.
SELECT YEAR(t.endDate) as m_Year, WEEK(t.endDate) as m_Week, count(*)
FROM table1 t
where t.`EndDate` BETWEEN DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
and CURDATE())
AND t.the_login = 'LOGIN 1'
GROUP BY m_Year,m_Week

Logic for dates in sql query for mysql

Hi i am totally confused with a date logic in my mysql query for a cron job to be run everyday at 12:00 AM
I am working on a auto listing website where the car listings are having a expiry date in mysql datetime format.
All the expired listings will be deleted from the website after 7 days from the datetime of the expiry
When the cron job will run it has do following things
Task 1 - Send an email alert to the users telling them that their listing has expired.
So I need to select all those listings which have expired since last time the cron job has been run and not include listings before that in order to send the expiry alert email only once per listing.
I tried following sql query for this task (Again confused with this as well)
SELECT car_id FROM cars WHERE expiry_date > DATE_SUB(NOW(), INTERVAL 1 DAY) AND expiry_date < NOW()
Task 2 - Will send an email alert to users telling them that listing is going to be permanently deleted after 24 hours.
So I need to select all those listings which are going to be deleted in more than 24 hours / 6 days have passed since they were expired and i need to make sure that they get minimum 24 hours time to renew them. Also i need to select / build the sql query in such a way that only those listings get selected which are going to expiry in 1 days and not other in order to avoid multiple email alerts instead of one time email alert
I tried following sql query for this task (I am totally confused with this query)
SELECT car_id FROM cars WHERE expiry_date > DATE_SUB(NOW(), INTERVAL 7 DAY) AND expiry_date < DATE_SUB(NOW(), INTERVAL 1 DAY)
Task 3 - Delete all the listings which were expired more than 7 days ago
I tried following sql query for this task
SELECT car_id FROM cars WHERE expiry_date < DATE_SUB(NOW(), INTERVAL 7 DAY)
Please help me in perfecting all the 3 queries so that the cron does it job exactly as i want. Also please let me where it has to >= (greater than or equal to) or <= (less than or equal to)
Here is the sqlfiddle table structure and couple of records (though they are not expired yet)
http://sqlfiddle.com/#!2/cfcdf
I will really appreciate the help.
Is this what you are looking for? Please try to add another column to see the differnce between expiry_date and current date time for you to get a better idea of the dates you are dealing with. Please look into some dates functions in MYSQL.
SQLFIDDLE DEMO
-- 3rd query expiry dates older than 7 days from
-- today
SELECT car_id, expiry_Date,
DATE_sub(NOW(), INTERVAL 7 DAY)
FROM cars
WHERE expiry_date <=
DATE_sub(NOW(), INTERVAL 7 DAY)
;
-- same
SELECT car_id, expiry_Date,
DATE_ADD(NOW(), INTERVAL -7 DAY)
FROM cars
WHERE expiry_date <=
DATE_ADD(NOW(), INTERVAL -7 DAY)
;
-- 2nd query going to expire in exactly 1 day
SELECT car_id, expiry_date,
Now() + interval 1 day
FROM cars
WHERE expiry_Date = Now() + interval 1 day
;
-- 1st query: expired
SELECT car_id FROM cars
WHERE expiry_date < Now()
;
-- 1st query: expired last 24 hours
SELECT car_id,DATEDIFF(expiry_date, Now())
FROM cars
WHERE expiry_Date < Now()
AND expiry_Date >= Now() - interval 1 day
;
Check out these queries
select * from cars where datediff(EXPIRY_DATE,now())=-1;
select * from cars where
datediff(DATE_ADD(EXPIRY_DATE, interval 24 hour),now())>=1 and
datediff(DATE_ADD(EXPIRY_DATE, interval 24 hour),now()) <=2;
select * from cars where datediff(expiry_date,now())<=-7;
ope they are working according to your need.
fiddle http://sqlfiddle.com/#!2/785ea/5
There is nothing significantly wrong with your queries, if you do not understand the functions that you have used then google them and read about them until you do.
There is a fundamental problem in your approach in that it relies on the cron job being run at exactly 24 hour intevals - to the milisecond - or there will be double ups and/or omissions.
You need another table to store details of when your batch program last ran; intitialise this with 1 row with a date a long time in the past so that we have a starting point.
You can get the most recent batch by SELECT MAX(date_ran) FROM BatchRecordTables. Store this in a local variable T0. Get the current time, store this in a local variable T1 (Do not use NOW() in multiple queries as they will be slightly differant times and you need them to be the same). I do not know the syntax for this is MySQL - you will have to look it up.
Your situations then become.
Send email to people whose listings have expired since that last time the cron job was run i.e. SELECT car_id FROM cars WHERE Expiry_Date BETWEEN T0 AND T1. This will only select people whoose listings have expired between this batch and the previous one.
For the second case, we need to know that these people have got the first email i.e. that their listing had expired before the last batch run so SELECT car_id FROM cars WHERE Expiry_Date BETWEEN DATE_SUB(T1, INTERVAL 6 DAY) AND T0. This will only select people whoose listings expired before the last batch (i.e. they got the exprired email) and more than 6 days ago.
Same logi applies - we want to know they got the second email. SELECT car_id FROM cars WHERE Expiry_Date BETWEEN DATE_SUB(T1, INTERVAL 7 DAY) AND DATE_SUB(T0, INTERVAL 6 DAY)
May I also suggest that you do not permenantly delete the listings but either copy them to a DeletedListings table or Flag them with a Deleted column - each has its own pros and cons. In the information age, never throw data away - you never know when it might be valuable.