MySQL Gets the current time, day before

Source: Internet
Author: User

Responsible for the project, using the MySQL database, on the page to show the number of registered people today, get the current month and year days, I use Curdate (),

Bad SQL statement

Eg:select COUNT (*) from USER WHERE registerdate >= curdate () and Registerdate < Curdate () +1;

Although the number obtained in the test environment is correct, but in the release to the line, found that sometimes the data is not query, the number is 0, therefore, the Internet query is not curdate () +1 is not standard, found that the MySQL official website is also not recognized time directly add and subtract, Although the current time will be converted to 20160802, this is the comparison of this string of characters, the MySQL official website is not recognized in this way to compare the size of the time, so:

The correct SQL statement

Eg:select COUNT (*) from the USER WHERE registerdate >= curdate () and Registerdate < Date_sub (Curdate (), INTERVAL-1 Day);
At this point, we use the date_sub () function, for example:


Today is August 01, 2016.

Date_sub (' 2016-08-01 ', Interval 1 day) says 2016-07-31
Date_sub (' 2016-08-01 ', Interval 0 day) says 2016-08-01
Date_sub (' 2016-08-01 ', interval-1 day) says 2016-08-02

Date_sub (Curdate (), Interval 1 day) represents 2016-07-31
Date_sub (Curdate (), Interval-1 Day) 2016-08-02
Date_sub (Curdate (), Interval 1 month) represents 2016-07-01
Date_sub (Curdate (), Interval-1 month) represents 2016-09-01
Date_sub (Curdate (), Interval 1 year) represents 2015-08-01
Date_sub (Curdate (), Interval-1 year) represents 2017-08-01


SELECT now (), Curdate (), Curtime ()

The results are similar:

Now () Curdate () Curtime ()
2016-08-01 16:25:462016-08-0116:25:46
Category: Oracle Database

MySQL Gets the current time, day before

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: 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.