Mysql statement where not exists with time intervals - php

I am basically trying to get my app to report when items are not returned or overdue in a 5 day and 15 day window. After much trial and error the below coded is the best I could come up with. However, when I extend to 15 days I still receive items that were returned (hence not exists not working appropriately) based off of movieid. The goal is see items from checkout that have not been returned with a time frames to notify members of late dates. Any suggestions to improve this statement?
select movieid, dueback
from checkout as a
where NOT exists
(
select * from returns as b
where a.movieid = b.movieid
AND dueback < DATE_SUB(NOW(), INTERVAL 5 DAY)
)
ORDER by dueback;
CHECKOUT TABLE: checkoutid, outdate, dueback, movieid, customerid, payment
RETURNS TABLE: returnid, today, movieid

You can use BETWEEN using the two computed dates. See Between documentation, but you can do something like:
AND dueback BETWEEN DATE_SUB(NOW(), INTERVAL 5 DAY) AND DATE_SUB(NOW(), INTERVAL 15 DAY)
That way you will get all dueback that is between 5 and 15 days from it's date.

Related

Fetch a data if the time duration between added date and current date is not greater than or equal to 2 hours

I am creating a booking management system. I got stuck in the booking cancellation part. I want to allow users to cancel their orders if their booking time and the current time duration is between 2 hours because I want to restrict the users to cancel their booking if their booking time and current time duration is greater than or equal to 2 hours.
I want to generate a query that returns all the bookings whose booking time is less than 2 hours. How can I achieve this?
This is my database structure.
SELECT * FROM `TableName` where TIMEDIFF(NOW(),Your_date_ColumnName) < '02:00:00.000000'
Assuming that booking_time is in MySQL standard format.
Try this and the below query will use index if you have one in booking_time column
SELECT *
FROM booking_table
WHERE booking_time BETWEEN CURRENT_TIMESTAMP() - INTERVAL 2 HOUR AND CURRENT_TIMESTAMP()
You can extract the hour part of your date. Refer to this link
Then using this query to get those less than 2 hours.
SELECT * FROM table1 WHERE tdate - 120 < EXTRACT(MINUTE FROM now())

MySQL fetch inactive user for n amount of days from created day

I am trying to fetch users that were inactive for N days from created date. I have dates stored in timestamp. I tried to pull it like that
SELECT name, created, accessed
FROM users
WHERE accessed >= DATE(NOW() - INTERVAL 3 DAY)
but it's not really working for me. I can't figure out how to build query to get inactive users that weren't active for N days from created.
Show users where last access was longer then 3 days ago:
SELECT
name,
created,
accessed
FROM users
WHERE ABS(TIMESTAMPDIFF(DAY, NOW(), accessed)) > 3
You can use DATE_ADD() to get inactive users by adding the N days to created column and then matching with accessed column. The example may be as:
SELECT name, created, accessed
FROM users
WHERE accessed >= DATE_ADD(created, INTERVAL 3 DAY)
DATE_SUB() uses to add time,days,years etc in specified date formated in timestamp.
For further study on this function please see here
https://www.w3schools.com/sql/func_date_add.asp
I Guess you can do here is
SELECT * from users where DATEDIFF(CURDATE(),accessed) > 3
CURDATE() - Current Date
accessed - last time account was accessed
3 - Difference of days.
Hope it Helps :)
Try changing NOW() with the column created. That way you are looking for users where accessed is more than the created date plus 3 days.
SELECT
name,
created,
accessed
FROM users
WHERE accessed >= DATE(created + INTERVAL 3 DAY)

Invoice query per 2 weeks. The best practice

I'm working on a invoice system for a application of mine. I want to invoice the users every 2 weeks. There is a cronjob every week with the check if the user gets an invoice. But it give me some bugs, because it has been a new year and the system gives someone a invoice even when they have got them a week ago.
This is my query:
SELECT *
FROM user
WHERE DAY(registered) = DAY(DATE_SUB(CURDATE(), INTERVAL 2 WEEK))
OR DAY(registered) = DAY(NOW()) AND registered != CURRENT_DATE()
Thank you!
You can try this- (code is tested) Demo
SELECT * FROM user WHERE registered > CURRENT_DATE() AND
MOD((FLOOR( DATEDIFF( now( ) , `registered` ))),14) = 0
The day function in MySQL returns the day of month. You will clearly have problems with your query when the time period spans month boundaries.
I think you want logic of this form:
SELECT *
FROM user
WHERE registered >= DATE_SUB(CURDATE(), INTERVAL 2 WEEK)) and
registered < curdate()
(Or <= if you want the current date to be invoiced.)
I do not know what this is doing:
OR DAY(registered) = DAY(NOW()) AND registered != CURRENT_DATE()
If the day of the month is the same for registered and now(), then registered should be the current date. The only difference between now() and curdate() is that the former includes the time stamp. I would also say that it is bad practice to mix three different ways of getting the same date in the same query, even if they are equivalent.

Filter an html table bound to a MySQL table by date

I have an html table that displays maintenance records. There are 3 columns that have dates in the future.
I want to have a button above the table that checks whether any of these 3 dates are within the next 30 days. If so, the row is displayed and other rows that are not of immediate concern are not displayed.
What is the best approach for achieving a filter like this?
Update: I'm trying to do it in a MySQL query.
I have 3 attributes in the SQL table that are date.
Does anyone know how to query whether the dates are within the next 30 days?
Does anyone know how to query whether the dates are within the next 30 days?
Use this :
WHERE yourdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
uses between, curdate() and date_add()
Update
to check multiple dates you need to do :
WHERE yourdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
AND yournextdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)
AND anotherdate BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 30 DAY)

MySQL: Getting results present in one time frame but not in another time frame

I have generated a dataset that contains data spanning thirty days. Im trying to issolate new data elements that have appeared in the last 2 days but not in the previous 28 days before that.
I run a PHP script that generates the test data. (PHP and MYSQL return the same time when tested)
I run the following query against it.
Results are returned accuretly for aproximetly half an hour. Then despite the fact I believe there to be matching records none are returned when running this query.
Is there any obvious mistake I'm making in the SQL that would cause this apparent 'drift' to occur?
About The Data:
The script generates a 'race' per day. It populates the ranking tables with ranking of the 10 'jokeys'. For the purposes of testing the script generates races from the previous 2 days with 2 new 'jokeys' in the top 10. The remaining 30 days the races are identical.
Results Expected:
The names of two jokeys who have recently ranked in a race (in the last two days and have not ranked in the previous 28).
The SQL:
SELECT *, FROM_UNIXTIME(`race_timestamp`) as ts FROM `rankings`
WHERE `race_venue` = UNHEX(MD5('someplace'))
AND `jokey` IN
(
SELECT `jokey`
FROM `rankings`
WHERE `race_timestamp`
BETWEEN # Get results for races between now and two days ago
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 DAY)) # timestamp two days ago
AND
UNIX_TIMESTAMP() # time stamp now
)
AND
`jokey` NOT IN
(SELECT `jokey`
FROM `rankings`
WHERE `race_timestamp`
BETWEEN # Get results between 2 and 30 days ago
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY)) # time stamp 30 days ago
AND
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 DAY)) # time stamp 2 days ago
)
GROUP BY jockey;
Hope someone can help! Ben
If you want to do this by date, rather than by the exact minute and second, you might change:
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))
to something like:
DATE(DATE_SUB(NOW(), INTERVAL 30 DAY))
Well there are some small errors in your SQL. You switch between jokey and jockey I'm going to just guess you mean jockey since this seems race-related. Not sure if this is in your actual code but it probably wouldn't run at all if it was. Also, you have no reason to use GROUP BY jockey as there are no aggregate functions being used.
Try this:
SELECT *, FROM_UNIXTIME(race_timestamp) AS ts FROM rankings
WHERE
race_venue = UNHEX(MD5('someplace'))
AND jockey IN (
SELECT jockey FROM rankings
WHERE race_timestamp
BETWEEN UNIX_TIMESTAMP(DATE_SUB(DATE(NOW()), INTERVAL 2 DAY))
AND UNIX_TIMESTAMP()
)
AND jockey NOT IN (
SELECT jockey FROM rankings
WHERE race_timestamp
BETWEEN UNIX_TIMESTAMP(DATE_SUB(DATE(NOW()), INTERVAL 30 DAY))
AND UNIX_TIMESTAMP(DATE_SUB(DATE(NOW()), INTERVAL 2 DAY))
)
Other than that, there are no actual errors I can detect in your logic. This query should return all jockeys for the requested venue who are ranked within the past 2 days and weren't ranked within 2-30 days ago.

Categories