"Mysql" to find items that have a difference of X days from the nearest, SQL statement about date

Source: Internet
Author: User

For a table of the following testtable, if you want to query a date that is 30 years from now


You should use the following SQL statement:

SELECT * FROM Testtablewheretimestampdiff (Year,date,now ()) <30

This method, even if the date field is a varchar type, can be queried successfully.

Timestampdiff function, the first field is a unit, can be changed to second,day,month, etc.

and the following query method, considering that the 1985 difference from the present 30, although simple, but if the date of change, such as next year, this statement will be changed to 1986-1-1.

At the same time, the most deadly is if the date field is saved for various reasons using the varchar type,

You will not be able to query success at all.

SELECT * from Testtablewhere date> ' 1985-1-1 '


"Mysql" to find items that have a difference of X days from the nearest, SQL statement about date

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.