I have expiry date I want to check the Expiry date is less than current or not. and if the Expiry date is ex:27-03-2011. I want to do some operation if the expiry is ex:27-04-2011 no need to do anything.
I want to check and开发者_如何学Python report the details for only one month, after one month of the expiry date I don't want to check how to do in query?
table1
field type
expdate date
Ex:2012-01-20
Please use MySQL date and time formats, they have the form YYYY-MM-DD (ISO date format). All MySQL Date and Time functions http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html work using this format.
MySQL also has date and time arithmetic. You can write expressions such as "NOW() - INTERVAL 5 day", which is also explained here http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
root@localhost [(none)]> select now(), now() - interval 5 day\G
*************************** 1. row ***************************
now(): 2011-03-28 13:49:24
now() - interval 5 day: 2011-03-23 13:49:24
1 row in set (0.00 sec)
To list all items older than 5 days, use
SELECT id FROM sometable WHERE created > NOW() - INTERVAL 5 DAY
or a similar query. There are a few things to take note of:
- an index on created should exist to make this fast
- the column name created is not part of an expression in order to enable it to be used with an index. We are specifically writing 'created > NOW() - INTERVAL 5 DAY' and not 'created + INTERVAL 5 DAY > NOW()' - this expression uses the column name created in an expression, so no index usage possible.
- if you are using this with a delete query, you are basically deleting old data from the left hand side of the time arrow, inserting new data at the right hand side of the time arrow. Maybe you want to use MySQL PARTITIONS and then PARTITION BY RANGE your table. That would allow you to delete old data by using ALTER TABLE DROP PARTITION throwing away the partition for say 5 days ago, creating a new one for tomorrow. This is much faster than DELETE in many cases.
精彩评论