How to get results greater than yesterday - mysql

I have a database that stores the date for each entry with the DATETIME format. I need to only retrieve results that are greater than yesterday. I say yesterday because I need the results for the current day and forward.
Currently I have the following.
$yest = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
$yest_date = date('n-j-o', $yest);
SELECT * FROM TBL_NAME WHERE DATE_FORMAT(event_date, \"%c-%d-%Y\") > '".$yest_date."'
That does not give any results even though I know there are events. Any help would be greatly appreciated.

SELECT * FROM tbl_name WHERE event_date > DATE_SUB(NOW(), INTERVAL 1 DAY)

WHERE event_date > date(now()) - 1

Select * from tbl_name WHERE event_date > DATE_SUB(curdate(), INTERVAL 1 DAY)
This should have it start from the beginning of yesterday rather than 24hours back from the time the query is run.
If you have this run in a cron you should probably verify the timezone of the database vs the server so that you don't run it for two days back intending to run at 12:01 but actually running at 11:01 due to variance.

SELECT * FROM TBL_NAME WHERE event_date >= CURDATE()

SELECT * FROM tbl_name WHERE field_date > now()- interval 1 day;

what this one does is to select values from the first minute today until last minute today.
WHERE scheduled_start_timestamp >= date() AND scheduled_start_timestamp <= date() + 1

Related

Delete records older than 24 hours or 1 day based on timestamp

I'm trying to write mysql query to delete records older than 24 hours.
The SELECT sql statement which i used is below
SELECT * FROM Request WHERE
timeStamp <= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))
Table contains lot of records older than 1 day but the result of this sql query is empty. Also it doesn't show any error message.
Timestamp field structure is
Name: timeSatamp
Type: timestamp
Default: CURRENT_TIMESTAMP
Can somebody help me to find out the mistake in this statement?
Thanks in advance!
You dont need the FROM_UNIXTIME() so this will do what you want
SELECT * FROM `ts` WHERE timeStamp <= DATE_SUB(NOW(), INTERVAL 1 DAY)
Or
SELECT * FROM `ts` WHERE timeStamp <= NOW() - INTERVAL 1 DAY
you can use DATEDIFF instead of the comparing from two date.
SELECT * FROM Request WHERE DATEDIFF(timestamp, NOW())>=1;
DATEDIFF returns a number of days between two date/timestamp.
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_datediff
Your Query should be like this:
SELECT * from Request where FROM_UNIXTIME(timeStamp) <= (NOW() - INTERVAL 1 DAY);
If the field timestamp contains 10 digits (like "1566836368") the right answer is
SELECT * from Request where FROM_UNIXTIME(timeStamp) <= (NOW() - INTERVAL 1 DAY);
like previously suggested by #akshaypjoshi
Delete 24 Hours older records using Mysql
DELETE FROM `table_name` WHERE `dateCreate` < (Now() - INTERVAL 24 HOUR);

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

mysql BETWEEN date range not working

I have a table called barcode_log, and these are all the datas from the table.
And now if I run this query
SELECT * FROM `barcode_log` WHERE barcode_log.assign_time BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) AND CURRENT_DATE;
I get this result
But it should return all the rows as all the data is within this month only. And assign_time field is stored as datetime. Any idea what i am doing wrong??
You are ignoring the time part (hh:mm:ss).
If the end day is set to the end timestamp of the current date then you can get the data of current day's too.
BETWEEN is inclusive
SELECT
*
FROM
`barcode_log`
WHERE
barcode_log.assign_time BETWEEN DATE_SUB(
CURRENT_DATE,
INTERVAL 30 DAY
)
AND TIMESTAMP(CONCAT(CURDATE(),' ','23:59:59'));
While the accepted answer works, there is a simpler solution. Just take the date part of the datetime column:
SELECT
*
FROM
`barcode_log`
WHERE
DATE(barcode_log.assign_time)
BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) AND CURRENT_DATE;
There is another way around: CAST() on barcode_log.assign_time field.
SELECT *
FROM `barcode_log`
WHERE CAST(barcode_log.assign_time AS DATE)
BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) AND CURRENT_DATE;
This excludes time from comparison and works fine for your purpose.

sql query to fetch the records of next 30 days

Here is my problem, I want to fetch next 30 days records from the table. I have a field in my table. For ex: In my table I have resource_date, In this column I have many records from 2013-02-05 to 2015-10-10. Say, If I logged into the website today(Today's Date is- 16/01/2015, It should fetch record for next 15 days and so on). How to do this? Thanks in advance
One way to do it
SELECT *
FROM table1
WHERE resource_date >= CURDATE() + INTERVAL 1 DAY -- skip today
AND resource_date < CURDATE() + INTERVAL 17 DAY -- 15 days starting tomorrow
Here is a SQLFiddle demo
In MySQL, you can use the NOW() function to get the current DATETIME, and the INTERVAL keyword to get intervals of time.
So, to get the records where resource_date is within the next 30 days, you would use:
SELECT *
FROM `my_table_name`
WHERE `resource_date` >= NOW()
AND `resource_date` < NOW() + INTERVAL 1 MONTH
;
In practice, you should rarely use SELECT *, and you should consider adding a LIMIT to this query to prevent your application from returning a result set that is "too large".
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
...
WHERE
'resource_date'> NOW() AND
'resource_date'< DATE_ADD(NOW(), INTERVAL 31 DAY);
Careful I think now() does minutes and hours so you miss a portion of a day.
WHERE resource_date >= CURDATE() AND resource_date <= DATE_ADD(CURDATE(), interval 15 DAY)

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'