Get users that have payment due in one day sql - mysql

This is probably easy to do but I can't seem to get my head around it. I have a user table that has a next_payment DATETIME column which gets update every month. I would like a query to get all the users where their next_payment DATETIME is in one day from the current datetime.
I tried something like this but it also gets me users where their next_payment is due in like 15 minutes. Not good
SELECT * FROM users WHERE next_payment >= NOW() AND next_payment <= NOW() + INTERVAL 1 DAY
I also tried something like this but this doesn't work either as it gives me all users that had next_payment datetime like 2 or 3 months ago (Not good).
SELECT * FROM users WHERE next_payment <= NOW() + INTERVAL 1 DAY
Thanks in advance for any help

Need to Cast the datetime field to date so you can include all data within the period and use between
SELECT * FROM
users
WHERE
cast(next_payment as date)
between cast(NOW() as date)
AND cast(NOW() + INTERVAL 1 DAY as date)

DATEDIFF works fine in MySQL:
SELECT
*
FROM
users
WHERE
DATEDIFF(next_payment, NOW()) = 1
SQL Fiddle for this example.

I would use:
SELECT * FROM users
WHERE TIMESTAMPDIFF(HOUR,NOW(),next_payment) <= 24

Related

How can I use DATEDIFF() to return rows where it has been more than 24 hours since it was added

I have table with column called date_added stored as datetime in MySQL.
I want to return rows where it has been 24 hours or more since it was added to the database.
I'm using the following query. However, it doesn't return what I want it to return.
SELECT * FROM campaign WHERE datediff(date_added,NOW())>=1
Here's what date_added in the Database looks like: 2017-08-15 00:48:31
You can do it this way
//This code is for SQL
SELECT *, DATEDIFF(HOUR,date_added,NOW()) AS Hours
FROM campaign WHERE DATEDIFF(HOUR,date_added,NOW()) >=24
As you can see i made the diff by hour
UPDATED
Try this one below.
SELECT * FROM campaign
WHERE HOUR(TIMEDIFF(DATE_FORMAT(date_added, "%Y-%m-%d %H:%i:%s"), DATE_FORMAT(NOW(), "%Y-%m-%d %H:%i:%s"))) >= 24
I used TimeDiff and used dateformat
and made it to hour.
You can also used this query
SELECT * FROM campaign WHERE TIMESTAMPDIFF(HOUR, date_added, NOW()) >= 24
Don't use any kind of "diff". Either of these will do match rows 'added' more than 24 hours ago.
WHERE date_added < NOW() - INTERVAL 1 DAY
WHERE date_added < NOW() - INTERVAL 24 HOUR
Since CURDATE() is midnight this morning, this will catch anything done anytime yesterday or today:
WHERE date_added < CURDATE() - INTERVAL 1 DAY

sql telling how many days have passed(datediff)

I'm working on a project where I want to display data after 3 days have passed.
What I'm having an issue with is getting the current date dynamically in php/sql. I'm aware of how to get the current date in php, but I dont know how to compare that value to the date that I have in the sql database.
You can do that directly in SQL
select * from your_table
where date_column <= curdate() - interval 3 day
You can use an interval select to limit the records to within 3 days if the column you're checking istimestamp, date, or datetime.
select * from tablename where timestamp_column >= NOW() - INTERVAL 3 DAY
You can use DATEDIFF function to check for days.
SELECT *
FROM tablename
WHERE DATEDIFF(CURRENT_DATE(), datecol) >= 3;

SELECT all records that are 30 days old

I need to SELECT all records that are 30 days old. I have the code below but it's not working. In updatestatus I have dates like 12/26/2011. I create a 30 day old date like
$onemonthago="01/01/2012";
$sth = $dbh->prepare(qq(
SELECT *
FROM people
WHERE STR_TO_DATE (updatestatus,'%m/%d/%y')
<= STR_TO_DATE ( "$onemonthago",'%m/%d/%Y')
) );
If the datatype of updatestatus is date:
SELECT *
FROM people
WHERE updatestatus <= '2012-01-01'
or:
SELECT *
FROM people
WHERE updatestatus <= CURRENT_DATE() - INTERVAL 1 MONTH
If the datatype is datetime or timestamp and you want to check the time part, too:
SELECT *
FROM people
WHERE updatestatus <= NOW() - INTERVAL 1 MONTH
You can put an exact datetime instead of the NOW() - INTERVAL 1 MONTH. The correct way depends on how you are storing the datetimes or timestamps (does the Perl code or MySQL creates them in the first place?).
You could also put - INTERVAL 30 DAY which yield slightly different results.
This is what I used. Very simple
$sth = $dbh->prepare(qq(SELECT * FROM people WHERE updatestatus + INTERVAL 30 DAY <= NOW() )) or die $DBI::errstr;
If the time column is in timestamp then use below query.(use from_unixtime function)
SELECT wd.* FROM `watchdog` as wd
WHERE from_unixtime(wd.timestamp) <= NOW() - INTERVAL 1 MONTH
You can try this way. In SQL, there is dateadd function and I think there should be similar function in MySQL.
select *
from Table
where str_to_date between dateadd(day,-30,getdate()) and getdate()
It retrieve records between current date and past 30 days. You need to adjust for time. If you don't count time, you need to remove timestamp.

gets rows from mysql where row elements date is between now & 5 days from now

I know this has prob been answered, but I've searched for almost an hour now and Im not finding my answer.
Here is my sql query.
SELECT *
FROM (`calendar_event`)
WHERE DATE_FORMAT(`start_time`, '%m/%d/%Y')
BETWEEN CURDATE() + INTERVAL 5 DAY AND CURDATE()
Here is the format of the start_time column 06/30/2011 8:30 AM
The query isn't having any errors, Im just not getting any results...
BETWEEN a AND b needs b to be greater than a, otherwise the interval is empty.
Try inverting the two date parameters you're building.
Try this:
WHERE date_col BETWEEN 'date1' and 'date2'

Select mysql query between date?

How to select data from mysql table past date to current date? For example, Select data from 1 january 2009 until current date ??
My column "datetime" is in datetime date type. Please help, thanks
Edit:
If let say i want to get day per day data from 1 january 2009, how to write the query? Use count and between function?
select * from *table_name* where *datetime_column* between '01/01/2009' and curdate()
or using >= and <= :
select * from *table_name* where *datetime_column* >= '01/01/2009' and *datetime_column* <= curdate()
All the above works, and here is another way if you just want to number of days/time back rather a entering date
select * from *table_name* where *datetime_column* BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
You can use now() like:
Select data from tablename where datetime >= "01-01-2009 00:00:00" and datetime <= now();
Late answer, but the accepted answer didn't work for me.
If you set both start and end dates manually (not using curdate()), make sure to specify the hours, minutes and seconds (2019-12-02 23:59:59) on the end date or you won't get any results from that day, i.e.:
This WILL include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02 23:59:59'
This WON'T include records from 2019-12-02:
SELECT *SOMEFIELDS* FROM *YOURTABLE* where *YOURDATEFIELD* between '2019-12-01' and '2019-12-02'