Return rows between two dates in MySQL - mysql

I would like to rows that have only been entered in the last 1 day.
I have a date column which stores YYYY-MM-DD, and I allow the user to send a date that they want to look at in this format yyymmdd how can I use this data to limit it to the previous day only?
I would imagine it is something to do with the BETWEEN keyword but I cant figure it out.

SELECT * from TABLE_NAME WHERE ROW_DATE BETWEEN '2011-03-20' AND '2011-03-21'

This query:
SELECT *
FROM mytable
WHERE mydate >= STR_TO_DATE('110321', '%y%m%d') - INTERVAL 1 DAY
AND mydate < STR_TO_DATE('110321', '%y%m%d')
will return all records for Mar 20, 2011

From the MySQL manual (here):
SELECT something FROM tbl_name WHERE DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= date_col;

SELECT * FROM YourTable WHERE date_column = DATE_ADD(CURDATE(), INTERVAL -1 DAY)
This returns all rows for today and yesterday.

Related

SQL Query by created 3 hours ago

Given a set of data with date_created stored like 2017-04-13 23:29:52, how would I construct an SQL query to select all items that were created within the last 3 hours?
I originally thought to do something like this:
SELECT
*,
MAX(date_created)
FROM items
GROUP BY date_created
but that would not be exactly what I want. I'm not sure how to go about this.
Use NOW() and INTERVAL in your WHERE clause
SELECT * FROM items WHERE date_created <= NOW() - INTERVAL 180 minute AND date_created >= NOW() - INTERVAL 210 minute
This one uses CURDATE, CURDATE and DATE_ADD:
SELECT *
FROM items
WHERE DATE(date_created) = CURDATE()
AND TIME(date_created) BETWEEN CURTIME() AND DATE_ADD(CURTIME(), INTERVAL -3 HOUR)
select * from items where
extract(hours from age(current_timestamp, date_created))>=3;
Extract keyword would extract hours difference from current timestamp and return only that is greater than or equal to 3.

Date difference in mySQL by certain number of days

There is a field due_date in my table. I want to get the records whose due_date is certain days ahead of today, i.e, the difference of due_date and CURDATE() can range between -1 to 7 (in days).
P.S.: -1 denotes that due_date was yesterday.
SELECT * FROM table WHERE (due_date - CURDATE() = 7);
Thanks in advance. :)
Use this query and this will fix your problem
SELECT *
FROM TABLENAME
WHERE due_date >= DATE_ADD(curdate(),INTERVAL -1 DAY)
AND due_date <= DATE_ADD(curdate(), INTERVAL 5 DAY);
You can use DATEDIFF
SELECT DATEDIFF('2014-11-30','2014-11-29') AS DiffDate
http://www.w3schools.com/SQl/func_datediff_mysql.asp
try this query:
SELECT * FROM table WHERE DATEDIFF(due_date, NOW()) <= 7 and DATEDIFF(due_date, NOW()) >= -1;

How to get data prior to one month of what user enters in SQL

current query
SELECT col1 FROM table1 where
id=1234 and (date(sys_time)
between "2015-07-01" AND "2015-07-10")
;
I want to get the data prior to one month from the dates mentioned
here. Is there any SQL query to do it?
Just use date_sub():
SELECT col1
FROM table1
WHERE id = 1234 AND
sys_time >= date_sub('2015-07-01', interval 1 month) and
sys_time < date_add(date_sub('2015-07-01', interval 1 month), interval 1 day)
Notice that I removed the date() function from sys_time. This helps MySQL use an index for the expression, if one is available:
Try this
SELECT GETDATE() AS CurrentDate, DATEADD(dd,-30,getdate())
You could set the "current date" and the previous date as parameters and include those in your query as the critera
Use DATE_ADD("2015-07-03", INTERVAL 1 MONTH) for the adding interval
Example :
SELECT a,b FROM table WHERE from_date between DATE_ADD("2015-07-01", INTERVAL 1 MONTH) AND DATE_ADD("2015-07-03", INTERVAL 1 MONTH)

How to filter data in mySQL query based on timestamp?

I want to fetch data of last 1 hour from mySQL database. I can certainly do it in java after results have been fetched but i don't want to unnecessarily put load on application. Please help.
select * from tb_data
where createdtime > ADDDATE(NOW(), INTERVAL -1 HOUR)
If the column is of type DATE you can use the DATE_SUB function, which substracts a given time interval from a DATE.
SELECT * FROM [table] WHERE [date_column] > DATE_SUB(NOW(), INTERVAL 1 HOUR)
Documentation
select * from yourtable where created_at between NOW() and date_sub(NOW() , interval 1 HOUR)

MySQL WHERE statement for finding expiry date within 2 days

I have a column named expiry which holds the expiry date of the item in timestamp format.
How would I execute a MySQL query to only select items with an expiry date within 2 days?
Thanks!!
SELECT * FROM table WHERE expiry BETWEEN(TODAY(), TODAY() + 2)
SELECT * FROM table WHERE DATEDIFF(expire, NOW()) <= 2;
select * from mytable
where expiry between now() and adddate(now(), INTERVAL 2 DAY);
SELECT ...
WHERE expiry <= NOW()+"2 days";
WHERE dateCol <= date_sub(now(), interval 2 day);