Select between dates issues - mysql

I have this SQL:
$sql="SELECT *
FROM table
WHERE expiresdate >= Date(Now())
AND expiresdate <= Date_add(Date(Now()), INTERVAL 10 day)
ORDER BY expiresdate ASC";
it should basically show all rows in the database that are going to expire within 10 days time however, lets say the expiredate was 2013-03-06 - this row will not display on any day after the expiredate
does anyone have any ideas?

This should be what you need:
SELECT
*
FROM
`table`
WHERE
expiresdate <= CURDATE() + INTERVAL 10 DAY
ORDER BY
expiresdate ASC

Related

Find records from MySQL which is updated within last hour but not created within last hour

I want to query from MySQL where posts will come only which is updated within last hour but not created in last hour.
Here is my MySQL query:
SELECT *
FROM
(SELECT *
FROM posts
WHERE (updated_at >= (NOW() - INTERVAL 30 MINUTE))) AS A
WHERE (created_at < DATE_SUB(NOW(), INTERVAL 60 MINUTE))
But this query doesn't give me right result.
I am attaching sample posts in CSV and adding result output from this query.
Sample: []
Query Output: []
I think you just need both conditions in a WHERE clause:
SELECT *
FROM posts
WHERE
updated_at >= DATE_SUB(NOW(), INTERVAL 1 HOUR) AND
created_at < DATE_SUB(NOW(), INTERVAL 1 HOUR);

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 FROM TABLE WHERE TIMESTAMP -> 21 Days from now

I have a table with some fields and a timestamp field named timestart.
What I would like to do is select all the records from my table where the field timestart is 21 days from now.
But how can I do this?
you can have this with. if you want exact equals to timestamp. use =
SELECT *
FROM table
WHERE date = DATE_ADD(NOW(), INTERVAL 21 DAY)
ORDER BY date DESC
you can achive the same by using
SELECT *
FROM table
WHERE date = DATE_ADD(NOW(), INTERVAL 21 DAY)
ORDER BY date DESC
The datediff function seems to meet the bill:
SELECT *
FROM my_table
WHERE DATE_DIFF (timestart, CURRENT_DATE()) >= 21
You can use this:
SELECT *
FROM table
WHERE date >= (NOW() - INTERVAL 21 DAY)
ORDER BY date DESC
LIMIT 20

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

Query to get all rows from previous month

I need to select all rows in my database that were created last month.
For example, if the current month is January, then I want to return all rows that were created in December, if the month is February, then I want to return all rows that were created in January. I have a date_created column in my database that lists the date created in this format: 2007-06-05 14:50:17.
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Here's another alternative. Assuming you have an indexed DATE or DATETIME type field, this should use the index as the formatted dates will be type converted before the index is used. You should then see a range query rather than an index query when viewed with EXPLAIN.
SELECT
*
FROM
table
WHERE
date_created >= DATE_FORMAT( CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01' )
AND
date_created < DATE_FORMAT( CURRENT_DATE, '%Y/%m/01' )
If there are no future dates ...
SELECT *
FROM table_name
WHERE date_created > (NOW() - INTERVAL 1 MONTH);
Tested.
Alternatively to hobodave's answer
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
You could achieve the same with EXTRACT, using YEAR_MONTH as unit, thus you wouldn't need the AND, like so:
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM date_created) = EXTRACT(YEAR_MONTH FROM CURDATE() - INTERVAL
1 MONTH)
SELECT *
FROM yourtable
where DATE_FORMAT(date_created, '%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 month),'%Y-%m')
This should return all the records from the previous calendar month, as opposed to the records for the last 30 or 31 days.
Even though the answer for this question has been selected already, however, I believe the simplest query will be
SELECT *
FROM table
WHERE
date_created BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND CURRENT_DATE();
WHERE created_date >= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY)
AND created_date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY)
This worked for me (Selects all records created from last month, regardless of the day you run the query this month)
Alternative with single condition
SELECT * FROM table
WHERE YEAR(date_created) * 12 + MONTH(date_created)
= YEAR(CURRENT_DATE) * 12 + MONTH(CURRENT_DATE) - 1
select fields FROM table
WHERE date_created LIKE concat(LEFT(DATE_SUB(NOW(), interval 1 month),7),'%');
this one will be able to take advantage of an index if your date_created is indexed, because it doesn't apply any transformation function to the field value.
Here is the query to get the records of the last month:
SELECT *
FROM `tablename`
WHERE `datefiled`
BETWEEN DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH )
AND
LAST_DAY( DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH ) )
Regards
- saqib
if you want to get orders from last month, you can try using
WHERE MONTH(order_date) = MONTH(CURRENT_DATE()) -1
One more way to do this in:
MYSQL
select * from <table_name> where date_created >= DATE_ADD(NOW(), INTERVAL -30 DAY);
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)