/ * --- 1) integer timestamp converted to a date format (yyyymmdd, yyyy-year mm month dd) - * /
SELECT FROM_UNIXTIME( 1249488000, '%Y%m%d' ); -- 20090806 SELECT FROM_UNIXTIME( 1249488000, '%Y年%m月%d' ); -- 2009年08月06
/ * --- 2) to convert datetime timestamp - * /
SELECT UNIX_TIMESTAMP(); SELECT UNIX_TIMESTAMP('2019-07-04 12:23:00');
The output format: 1562214180
/ * --- 3) acquires the current time (yyyy-mm-dd hh: mm: ss) method - * /
SELECT NOW(); SELECT LOCALTIME(); SELECT LOCALTIME; SELECT SYSDATE();
The output format: 2019-07-04 22:15:49
/ * --- 4) to get the current date (date) - * /
SELECT CURDATE(); SELECT CURRENT_DATE(); SELECT CURRENT_DATE;
The output format: 2019-07-04
/ * --- 5) to get the current time (time) - * /
SELECT CURTIME(); SELECT CURRENT_TIME(); SELECT CURRENT_TIME;
The output format: 22: 13:56
/ * --- 6) to get the current date one day before - * /
SELECT DATE_SUB(CURDATE(),INTERVAL 1 DAY);
The output format: 2019-07-03
/ * - 7) Application: Get within 24 hours, the amount of data within 7 days 30 days --- * /
- 24 hours the SELECT COUNT ( . 1 ) the FROM table1 A the WHERE TO_DAYS (time field ``) = TO_DAYS (the NOW ()); - . 7 days the SELECT COUNT ( . 1 ) the FROM table1 A the WHERE for DATE_SUB (CURDATE (), the INTERVAL . 7 DAY ) <= DATE ( `` time field); - 30 days the SELECT COUNT ( . 1 ) the FROM table1 A the WHERE for DATE_SUB (CURDATE (), the INTERVAL 30 DAY ) <= DATE ( `` time field);
/ * --- 8) acquired before the current time of 5 minutes - * /
- 5 minutes before the SELECT DATE_ADD (the NOW (), the INTERVAL - . 5 MINUTE); - 5 min after the SELECT DATE_ADD (the NOW (), the INTERVAL . 5 MINUTE);
/ * --- 9) Application: query data within 5 minutes after the current time in the data or the first five minutes - * /
select * from table1 where ((`时间字段` BETWEEN DATE_ADD(NOW(), INTERVAL -5 MINUTE) AND NOW()) OR (`时间字段` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 MINUTE)));
/ * --- 10) query specified period of record BETWEEN AND - * /
. 1 the SELECT * the FROM table1 the WHERE time field of the BETWEEN ' 2010-7-12 11:18:54 ' the AND ' 2010-7-12 11:22:20 ' ;