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