SQL query to show rows that are 60+ in the past - php

I have a row in my database with the datetime as
2013-11-07 13:04:57
then i am running this Query:
SELECT * from customer_communication
WHERE customer_seq = '276'
and datetime < DATE_SUB(DATE(now()), INTERVAL 60 DAY)
order by datetime DESC
so it should not be returning this row
i want to show all rows with a datetime that are 60+ days in the past

You can use DATEDIFF function
For example,
DATEDIFF(date1, date2) < 60

The date 2013-11-07 13:04:57 is more than 60 days in the past:
SELECT DATE_SUB(DATE(NOW()), INTERVAL 60 DAY);
=> 2013-11-10
Any date on or before November 10th will be displayed (from January 9th)
# Show difference in days.
SELECT DATEDIFF(DATE(NOW()),'2013-11-07');
=> 63
Additionally, you should be using the <= comparator if you wish to show rows that have a date set to exactly 60 days ago from the current day.

The problem is that your current query will select rows that are older than one datetime value. You need to change comparison sign to:
datetime > DATE_SUB(DATE(now()), INTERVAL 60 DAY)
And date 2013-11-07 13:04:57 more than 60 days in the past.

Related

How to get last 3 days records from database without today?

I am using this code:
WHERE date > DATE_SUB(CURDATE(), INTERVAL 3 DAY)
And result will be like this:
2022-05-12
2022-05-11
2022-05-10
But I want this:
2022-05-11
2022-05-10
2022-05-09
Use a range here:
WHERE date < CURDATE() AND date >= DATE_SUB(CURDATE(), INTERVAL 3 DAY)
Assuming today's date be 2022-05-12, the above logic would exclude this date but include the three previous days, from 11th May to 9th May.

Sql query to get row from date filled

I want to get data from training_course table where current date minus 5 days is equal to training_end_date. Training_end_date is my field in the table.
Thanks
You seem to want:
select *
from training_course
where training_end_date = current_date - interval 5 day
Or, if your dates have time components, you maybe want:
select *
from training_course
where training_end_date >= current_date - interval 5 day and training_date < current_date - interval 4 day

Select date with n days from the date added

I have a mysql table with the following fields
Name | Email | Date | Status
I want to extract the records where date range is between 30 days
Assume today is 2014/12/9
ie. date values are
2014/11/25
2014/12/2
2014/12/1
2014/10/25
2014/11/9
I need the o/p as (the number of days should be with in 30 days from the db date to today date)
2014/11/25
2014/12/2
2014/12/1
2014/11/9
I want to extract records those have the interval of less than 30 days from the date in the db.
Yes. I want to fetch the record between 2 days. For this I used this query
SELECT * FROM tbl_jobboard WHERE dtDate <= ( dtDate +30 )
But it is not working.
How to write the select query?
USE DATE_SUB like this:
SELECT * FROM table1
WHERE `date` BETWEEN DATE_SUB(CURDATE(),INTERVAL 30 DAY) AND CURDATE()
Working Fiddle Demo: http://sqlfiddle.com/#!2/6344f2/1
use following query
select * from table_Name t where t.date<=now() and t.date>=DATE_SUB(now(),
INTERVAL 31 DAY)

selecting all rows 7 days from now based on unix timestamp and current date

I have a table with the following fields:
id - int
name - int
the_date - int
the_date is a unix timestamp for when the row was added
Now I am trying to write a query that will select all rows on the day that is 7 days from now. I'm not talking about select the rows >= 7 days from time(), I need to grab the current day using time(), and then run a SELECT that grabs all the rows that were inserted on the day that is 7 days from the time().
I know how to do it so its within 7 days from the time() with a simple >= SELECT, but I don't know how to do it so it selects all rows whose unix timestamp is on that particular day (7 days from now).
Any help would be greatly appreciated.
The points of intrest here,
i use curdate() to get the current DATE, not DATETIME.
i add 7 days and convert to unix time, which yields the starting second of that day
i do the same for the next day
i structure the where clause to be >= or equal to the target day, but < the start of the next day. This gives a range of seconds that fully covers the target day.
i dont use any functions on the column itself. This is important because if i did, mysql wouldn't be abl;e to use any indexes that exist on the column to fullfill the query.
where the_date >= unix_timestamp(curdate() + interval 7 day)
and the_date < unix_timestamp(curdate() + interval 8 day)
SELECT * FROM `dbname1`.`test`
WHERE
date(FROM_UNIXTIME(`the_date`)) = ADDDATE(DATE(NOW()), INTERVAL 7 DAY);
$todayParts = getdate();
$startToday = mktime(0, 0, 0, $todayParts['mon'], $todayParts['mday'], $todayParts['year']);
$sevebDaysFromNow = 60 * 60 * 24 * 7;
$seventhStart = $startToday + $sevebDaysFromNow;
$seventhEnd = $seventhStart + 60 * 60 * 24;
$sql = "SELECT * FROM <table> WHERE the_date BETWEEN $seventhStart AND $seventEnd";
This will calculate 7 days from the start of the day you are now. Hope this helps

Mysql Query back to midnight only

I have this query that looks up results from a database for the last twenty minutes, now i know how to look up in hours, days, etc, but is it possible to look up only as far back as midnight of that day. so when ever the query is run and what ever time it only looks back as far as midnight?
SELECT * FROM ip_stats WHERE date >= ( NOW() - INTERVAL 20 MINUTE ) and ip='$ip'
This is my code but is there away in which i can replace the interval for a specific time.
Any help would be appreciated.
Looking back to midnight of the current day is the same as looking at the current date with no time component. You can therefore use DATE() to truncate the datetime column date to only the date portion, and compare it to CURDATE().
SELECT * FROM ip_stats WHERE DATE(date) = CURDATE() and ip='$ip'
SELECT * FROM ip_stats WHERE date >= ( NOW() - INTERVAL 20 MINUTE ) AND date >= CURDATE() and ip='$ip'
Just make the column date be bigger than the curdate (which is the starting of this day).
SELECT * FROM ip_stats
WHERE date >= ( NOW() - INTERVAL 20 MINUTE )
and date > CURDATE()
and ip='$ip'
You can use CURDATE() to get rows since midnight of the current day:
SELECT * FROM ip_stats WHERE date >= CURDATE() and ip='$ip'
SELECT *
FROM ip_stats
WHERE date >= GREATEST( NOW() - INTERVAL 20 MINUTE, CURRENT_DATE )
AND ip = '$ip'

Categories