MySQL WHERE statement for finding expiry date within 2 days - mysql

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);

Related

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;

Select last months records mysql from timestamp column

I have a mysql DB that has a TIMESTAMP field titled date. How can I select all fields where the last month ?
Forexample 01-05-2014 then 31-05-2014 etc..
Thanks in advance!
select * from table where timestamp_col=LAST_DAY(timestamp_col)
Source
As per my understanding do you want this like...
SELECT * FROM table_name where timestamp_col BETWEEN '2014-05-01' AND '2014-05-31';
SELECT DATETİME, FROM_UNIXTIME(datetime) TİMESTAMP
FROM table_name
WHERE DATETİME >= UNIX_TIMESTAMP( DATE_ADD(LAST_DAY(NOW() + INTERVAL 1 DAY - INTERVAL 2 MONTH) , INTERVAL 1 DAY ))
AND DATETİME < UNIX_TIMESTAMP( (LAST_DAY(NOW() + INTERVAL 1 DAY - 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)

select dates that are between current date and 3 month from the current date in mysql

I want to select all dates that are between the current date and 3 months before.
I tried using this query but it isn't working right.
$sql = mysql_query("
SELECT *
FROM date
WHERE d_date BETWEEN NOW() AND NOW() - INTERVAL 3 MONTH
");
Please if you could help me write the right syntax.
You need to swap your bounaries, and it will work:
SELECT * FROM date
WHERE d_date BETWEEN now() - INTERVAL 3 MONTH AND now()
For example, this query returns true (SQLFiddle):
SELECT (now() - interval 1 month)
BETWEEN now() - interval 3 month AND now()
SELECT * FROM Table
WHERE anydate_col BETWEEN NOW() AND DATE_ADD( NOW() , INTERVAL +3 MONTH)

Return rows between two dates in 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.