{"id":1113,"date":"2010-07-01T16:08:00","date_gmt":"2010-07-01T08:08:00","guid":{"rendered":"\/?p=1113"},"modified":"2010-07-01T16:08:00","modified_gmt":"2010-07-01T16:08:00","slug":"1113","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/mysql\/1113.html","title":{"rendered":"MySQL\u65f6\u95f4\u51fd\u6570\u7528\u6cd5\u4ee3\u7801\u4e3e\u4f8b"},"content":{"rendered":"
\u4eca\u5929\u540c\u4e8b\u4e34\u65f6\u63d0\u51fa\u4e00\u4e2a\u5c0f\u9700\u6c42\uff0c\u56e0\u4e3a\u540e\u53f0\u4e0d\u662f\u5f88\u5b8c\u5584\uff0c\u8fd9\u6837\u529f\u80fd\u53ea\u80fd\u5230\u6570\u636e\u5e93\u91cc\u9762\u76f4\u63a5\u8fd0\u884cSQL\u8bed\u53e5\u4e86\uff0c\u53d1\u73b0\u51e0\u4e2a\u51fd\u6570\u8fd8\u662f\u633a\u597d\u7528\u7684\u3002\u3002\u3002<\/span><\/p>\n select count(*) from user where create_time >= UNIX_TIMESTAMP(‘2010-05-13’) and create_time <= UNIX_TIMESTAMP(‘2010-07-01’);<\/span><\/p>\n \u5bf9\u4e8e\u6bcf\u4e2a\u7c7b\u578b\u62e5\u6709\u7684\u503c\u8303\u56f4\u4ee5\u53ca\u5e76\u4e14\u6307\u5b9a\u65e5\u671f\u4f55\u65f6\u95f4\u503c\u7684\u6709\u6548\u683c\u5f0f\u7684\u63cf\u8ff0\u89c17.3.6 \u65e5\u671f\u548c\u65f6\u95f4\u7c7b\u578b\u3002 <\/p>\n \u8fd9\u91cc\u662f\u4e00\u4e2a\u4f7f\u7528\u65e5\u671f\u51fd\u6570\u7684\u4f8b\u5b50\u3002\u4e0b\u9762\u7684\u67e5\u8be2\u9009\u62e9\u4e86\u6240\u6709\u8bb0\u5f55\uff0c\u5176date_col\u7684\u503c\u662f\u5728\u6700\u540e30\u5929\u4ee5\u5185\uff1a <\/p>\n mysql> SELECT something FROM table DAYOFWEEK(date)<\/strong> WEEKDAY(date)<\/strong> DAYOFMONTH(date)<\/strong> DAYOFYEAR(date)<\/strong> MONTH(date) DAYNAME(date) MONTHNAME(date)<\/strong> QUARTER(date)<\/strong> WEEK(date)<\/strong> YEAR(date)<\/strong> HOUR(time)<\/strong> MINUTE(time) SECOND(time)<\/strong> PERIOD_ADD(P,N)<\/strong> PERIOD_DIFF(P1,P2)<\/strong> DATE_ADD(date,INTERVAL expr type)<\/strong> MySQL\u5728expr\u683c\u5f0f\u4e2d\u5141\u8bb8\u4efb\u4f55\u6807\u70b9\u5206\u9694\u7b26\u3002\u8868\u793a\u663e\u793a\u7684\u662f\u5efa\u8bae\u7684\u5206\u9694\u7b26\u3002\u5982\u679cdate\u53c2\u6570\u662f\u4e00\u4e2aDATE\u503c\u5e76\u4e14\u4f60\u7684\u8ba1\u7b97\u4ec5\u4ec5\u5305\u542bYEAR\u3001MONTH \u548cDAY\u90e8\u5206(\u5373\uff0c\u6ca1\u6709\u65f6\u95f4\u90e8\u5206)\uff0c\u7ed3\u679c\u662f\u4e00\u4e2aDATE\u503c\u3002\u5426\u5219\u7ed3\u679c\u662f\u4e00\u4e2aDATETIME\u503c\u3002 <\/p>\n mysql> SELECT "1997-12-31 23:59:59" + INTERVAL 1 SECOND; \u5982\u679c\u4f60\u6307\u5b9a\u592a\u77ed\u7684\u95f4\u9694\u503c(\u4e0d\u5305\u62ectype\u5173\u952e\u8bcd\u671f\u671b\u7684\u95f4\u9694\u90e8\u5206)\uff0cMySQL\u5047\u8bbe\u4f60\u7701\u6389\u4e86\u95f4\u9694\u503c\u7684\u6700\u5de6\u9762\u90e8\u5206\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6307\u5b9a\u4e00\u4e2atype\u662f DAY_SECOND\uff0c\u503cexpr\u88ab\u5e0c\u671b\u6709\u5929\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u90e8\u5206\u3002\u5982\u679c\u4f60\u8c61"1:10"\u8fd9\u6837\u6307\u5b9a\u503c\uff0cMySQL\u5047\u8bbe\u65e5\u5b50\u548c\u5c0f\u65f6\u90e8\u5206\u662f\u4e22\u5931\u7684\u5e76\u4e14\u503c\u4ee3\u8868\u5206\u949f\u548c\u79d2\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c"1:10" DAY_SECOND\u4ee5\u5b83\u7b49\u4ef7\u4e8e"1:10" MINUTE_SECOND\u7684\u65b9\u5f0f\u89e3\u91ca\uff0c\u8fd9\u5bf9\u90a3MySQL\u89e3\u91caTIME\u503c\u8868\u793a\u7ecf\u8fc7\u7684\u65f6\u95f4\u800c\u975e\u4f5c\u4e3a\u4e00\u5929\u7684\u65f6\u95f4\u7684\u65b9\u5f0f\u6709\u4e8c\u4e49\u6027\u3002\u5982\u679c\u4f60\u4f7f\u7528\u786e\u5b9e\u4e0d\u6b63\u786e\u7684\u65e5\u671f\uff0c\u7ed3\u679c\u662fNULL\u3002\u5982\u679c\u4f60\u589e\u52a0MONTH\u3001YEAR_MONTH\u6216YEAR\u5e76\u4e14\u7ed3\u679c\u65e5\u671f\u5927\u4e8e\u65b0\u6708\u4efd\u7684\u6700\u5927\u503c\u5929\u6570\uff0c\u65e5\u5b50\u5728\u65b0\u6708\u7528\u6700\u5927\u7684\u5929\u8c03\u6574\u3002 <\/p>\n mysql> select DATE_ADD(‘1998-01-30’, Interval 1 month); \u6ce8\u610f\uff0c\u4ece\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\u8bcdINTERVAL\u548ctype\u5173\u952e\u8bcd\u4e0d\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\u3002 <\/p>\n TO_DAYS(date)<\/strong> TO_DAYS()<\/strong> FROM_DAYS(N)<\/strong> DATE_FORMAT(date,format)<\/strong> \u6240\u6709\u7684\u5176\u4ed6\u5b57\u7b26\u4e0d\u505a\u89e3\u91ca\u88ab\u590d\u5236\u5230\u7ed3\u679c\u4e2d\u3002 <\/p>\n mysql> select DATE_FORMAT(‘1997-10-04 22:23:00’, ‘%W %M %Y’); TIME_FORMAT(time,format) CURDATE()<\/strong> CURTIME()<\/strong> NOW()<\/strong> UNIX_TIMESTAMP()<\/strong> FROM_UNIXTIME(unix_timestamp)<\/strong> FROM_UNIXTIME(unix_timestamp,format)<\/strong> SEC_TO_TIME(seconds)<\/strong> TIME_TO_SEC(time)<\/strong> \u4eca\u5929\u540c\u4e8b\u4e34\u65f6\u63d0\u51fa\u4e00\u4e2a\u5c0f\u9700\u6c42\uff0c\u56e0\u4e3a\u540e\u53f0\u4e0d\u662f\u5f88\u5b8c\u5584\uff0c\u8fd9\u6837\u529f\u80fd\u53ea\u80fd\u5230\u6570\u636e\u5e93\u91cc\u9762\u76f4\u63a5\u8fd0\u884cSQL\u8bed\u53e5\u4e86\uff0c\u53d1\u73b0\u51e0\u4e2a\u51fd\u6570\u8fd8\u662f\u633a\u597d\u7528\u7684\u3002\u3002\u3002<\/span><\/p>\n select count(*) from user where create_time >= UNIX_TIMESTAMP(‘2010-05-13’) and create_time <= UNIX_TIMESTAMP(‘2010-07-01’);<\/span><\/p>\n \u5bf9\u4e8e\u6bcf\u4e2a\u7c7b\u578b\u62e5\u6709\u7684\u503c\u8303\u56f4\u4ee5\u53ca\u5e76\u4e14\u6307\u5b9a\u65e5\u671f\u4f55\u65f6\u95f4\u503c\u7684\u6709\u6548\u683c\u5f0f\u7684\u63cf\u8ff0\u89c17<\/span><\/p>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1113"}],"collection":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/comments?post=1113"}],"version-history":[{"count":0,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1113\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/media?parent=1113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/categories?post=1113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/tags?post=1113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
WHERE TO_DAYS(NOW()) – TO_DAYS(date_col) <= 30; <\/p>\n
\u8fd4\u56de\u65e5\u671fdate\u7684\u661f\u671f\u7d22\u5f15(1=\u661f\u671f\u5929\uff0c2=\u661f\u671f\u4e00, \u2026\u20267=\u661f\u671f\u516d)\u3002\u8fd9\u4e9b\u7d22\u5f15\u503c\u5bf9\u5e94\u4e8eODBC\u6807\u51c6\u3002
mysql> select DAYOFWEEK(‘1998-02-03’);
-> 3 <\/p>\n
\u8fd4\u56dedate\u7684\u661f\u671f\u7d22\u5f15(0=\u661f\u671f\u4e00\uff0c1=\u661f\u671f\u4e8c, \u2026\u20266= \u661f\u671f\u5929)\u3002
mysql> select WEEKDAY(‘1997-10-04 22:23:00’);
-> 5
mysql> select WEEKDAY(‘1997-11-05’);
-> 2 <\/p>\n
\u8fd4\u56dedate\u7684\u6708\u4efd\u4e2d\u65e5\u671f\uff0c\u57281\u523031\u8303\u56f4\u5185\u3002
mysql> select DAYOFMONTH(‘1998-02-03’);
-> 3 <\/p>\n
\u8fd4\u56dedate\u5728\u4e00\u5e74\u4e2d\u7684\u65e5\u6570, \u57281\u5230366\u8303\u56f4\u5185\u3002
mysql> select DAYOFYEAR(‘1998-02-03’);
-> 34 <\/p>\n
<\/strong>\u8fd4\u56dedate\u7684\u6708\u4efd\uff0c\u8303\u56f41\u523012\u3002
mysql> select MONTH(‘1998-02-03’);
-> 2 <\/p>\n
<\/strong>\u8fd4\u56dedate\u7684\u661f\u671f\u540d\u5b57\u3002
mysql> select DAYNAME("1998-02-05");
-> ‘Thursday’ <\/p>\n
\u8fd4\u56dedate\u7684\u6708\u4efd\u540d\u5b57\u3002
mysql> select MONTHNAME("1998-02-05");
-> ‘February’ <\/p>\n
\u8fd4\u56dedate\u4e00\u5e74\u4e2d\u7684\u5b63\u5ea6\uff0c\u8303\u56f41\u52304\u3002
mysql> select QUARTER(’98-04-01′);
-> 2 <\/p>\n
\u3000
WEEK(date,first)<\/strong>
\u5bf9\u4e8e\u661f\u671f\u5929\u662f\u4e00\u5468\u7684\u7b2c\u4e00\u5929\u7684\u5730\u65b9\uff0c\u6709\u4e00\u4e2a\u5355\u4e2a\u53c2\u6570\uff0c\u8fd4\u56dedate\u7684\u5468\u6570\uff0c\u8303\u56f4\u57280\u523052\u30022\u4e2a\u53c2\u6570\u5f62\u5f0fWEEK()\u5141\u8bb8\u4f60\u6307\u5b9a\u661f\u671f\u662f\u5426\u5f00\u59cb\u4e8e\u661f\u671f\u5929\u6216\u661f\u671f\u4e00\u3002\u5982\u679c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f0\uff0c\u661f\u671f\u4ece\u661f\u671f\u5929\u5f00\u59cb\uff0c\u5982\u679c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f1\uff0c\u4ece\u661f\u671f\u4e00\u5f00\u59cb\u3002
mysql> select WEEK(‘1998-02-20’);
-> 7
mysql> select WEEK(‘1998-02-20’,0);
-> 7
mysql> select WEEK(‘1998-02-20’,1);
-> 8 <\/p>\n
\u8fd4\u56dedate\u7684\u5e74\u4efd\uff0c\u8303\u56f4\u57281000\u52309999\u3002
mysql> select YEAR(’98-02-03′);
-> 1998 <\/p>\n
\u8fd4\u56detime\u7684\u5c0f\u65f6\uff0c\u8303\u56f4\u662f0\u523023\u3002
mysql> select HOUR(’10:05:03′);
-> 10 <\/p>\n
<\/strong>\u8fd4\u56detime\u7684\u5206\u949f\uff0c\u8303\u56f4\u662f0\u523059\u3002
mysql> select MINUTE(’98-02-03 10:05:03′);
-> 5 <\/p>\n
\u56de\u6765time\u7684\u79d2\u6570\uff0c\u8303\u56f4\u662f0\u523059\u3002
mysql> select SECOND(’10:05:03′);
-> 3 <\/p>\n
\u589e\u52a0N\u4e2a\u6708\u5230\u9636\u6bb5P\uff08\u4ee5\u683c\u5f0fYYMM\u6216YYYYMM)\u3002\u4ee5\u683c\u5f0fYYYYMM\u8fd4\u56de\u503c\u3002\u6ce8\u610f\u9636\u6bb5\u53c2\u6570P\u4e0d\u662f\u65e5\u671f\u503c\u3002
mysql> select PERIOD_ADD(9801,2);
-> 199803 <\/p>\n
\u8fd4\u56de\u5728\u65f6\u671fP1\u548cP2\u4e4b\u95f4\u6708\u6570\uff0cP1\u548cP2\u5e94\u8be5\u4ee5\u683c\u5f0fYYMM\u6216YYYYMM\u3002\u6ce8\u610f\uff0c\u65f6\u671f\u53c2\u6570P1\u548cP2\u4e0d\u662f\u65e5\u671f\u503c\u3002
mysql> select PERIOD_DIFF(9802,199703);
-> 11 <\/p>\n
\u3000
DATE_SUB(date,INTERVAL expr type)<\/strong>
\u3000
ADDDATE(date,INTERVAL expr type)<\/strong>
\u3000
SUBDATE(date,INTERVAL expr type)<\/strong>
\u8fd9\u4e9b\u529f\u80fd\u6267\u884c\u65e5\u671f\u8fd0\u7b97\u3002\u5bf9\u4e8eMySQL 3.22\uff0c\u4ed6\u4eec\u662f\u65b0\u7684\u3002ADDDATE()\u548cSUBDATE()\u662fDATE_ADD()\u548cDATE_SUB()\u7684\u540c\u4e49\u8bcd\u3002
\u5728MySQL 3.23\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528+\u548c-\u800c\u4e0d\u662fDATE_ADD()\u548cDATE_SUB()\u3002\uff08\u89c1\u4f8b\u5b50\uff09date\u662f\u4e00\u4e2a\u6307\u5b9a\u5f00\u59cb\u65e5\u671f\u7684
DATETIME\u6216DATE\u503c\uff0cexpr\u662f\u6307\u5b9a\u52a0\u5230\u5f00\u59cb\u65e5\u671f\u6216\u4ece\u5f00\u59cb\u65e5\u671f\u51cf\u53bb\u7684\u95f4\u9694\u503c\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0cexpr\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1b\u5b83\u53ef\u4ee5\u4ee5
\u4e00\u4e2a“-”\u5f00\u59cb\u8868\u793a\u8d1f\u95f4\u9694\u3002type\u662f\u4e00\u4e2a\u5173\u952e\u8bcd\uff0c\u6307\u660e\u8868\u8fbe\u5f0f\u5e94\u8be5\u5982\u4f55\u88ab\u89e3\u91ca\u3002EXTRACT(type FROM date)\u51fd\u6570\u4ece\u65e5\u671f
\u4e2d\u8fd4\u56de“type”\u95f4\u9694\u3002\u4e0b\u8868\u663e\u793a\u4e86type\u548cexpr\u53c2\u6570\u600e\u6837\u88ab\u5173\u8054\uff1a type\u503c \u542b\u4e49 \u671f\u671b\u7684expr\u683c\u5f0f
SECOND \u79d2 SECONDS
MINUTE \u5206\u949f MINUTES
HOUR \u65f6\u95f4 HOURS
DAY \u5929 DAYS
MONTH \u6708 MONTHS
YEAR \u5e74 YEARS
MINUTE_SECOND \u5206\u949f\u548c\u79d2 "MINUTES:SECONDS"
HOUR_MINUTE \u5c0f\u65f6\u548c\u5206\u949f "HOURS:MINUTES"
DAY_HOUR \u5929\u548c\u5c0f\u65f6 "DAYS HOURS"
YEAR_MONTH \u5e74\u548c\u6708 "YEARS-MONTHS"
HOUR_SECOND \u5c0f\u65f6, \u5206\u949f\uff0c "HOURS:MINUTES:SECONDS"
DAY_MINUTE \u5929, \u5c0f\u65f6, \u5206\u949f "DAYS HOURS:MINUTES"
DAY_SECOND \u5929, \u5c0f\u65f6, \u5206\u949f, \u79d2 "DAYS HOURS:MINUTES:SECONDS" <\/p>\n
-> 1998-01-01 00:00:00
mysql> SELECT INTERVAL 1 DAY + "1997-12-31";
-> 1998-01-01
mysql> SELECT "1998-01-01" – INTERVAL 1 SECOND;
-> 1997-12-31 23:59:59
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",
INTERVAL 1 SECOND);
-> 1998-01-01 00:00:00
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",
INTERVAL 1 DAY);
-> 1998-01-01 23:59:59
mysql> SELECT DATE_ADD("1997-12-31 23:59:59",
INTERVAL "1:1" MINUTE_SECOND);
-> 1998-01-01 00:01:00
mysql> SELECT DATE_SUB("1998-01-01 00:00:00",
INTERVAL "1 1:1:1" DAY_SECOND);
-> 1997-12-30 22:58:59
mysql> SELECT DATE_ADD("1998-01-01 00:00:00",
INTERVAL "-1 10" DAY_HOUR);
-> 1997-12-30 14:00:00
mysql> SELECT DATE_SUB("1998-01-02", INTERVAL 31 DAY);
-> 1997-12-02
mysql> SELECT EXTRACT(YEAR FROM "1999-07-02");
-> 1999
mysql> SELECT EXTRACT(YEAR_MONTH FROM "1999-07-02 01:02:03");
-> 199907
mysql> SELECT EXTRACT(DAY_MINUTE FROM "1999-07-02 01:02:03");
-> 20102 <\/p>\n
-> 1998-02-28 <\/p>\n
\u7ed9\u51fa\u4e00\u4e2a\u65e5\u671fdate\uff0c\u8fd4\u56de\u4e00\u4e2a\u5929\u6570(\u4ece0\u5e74\u7684\u5929\u6570)\u3002
mysql> select TO_DAYS(950501);
-> 728779
mysql> select TO_DAYS(‘1997-10-07’);
-> 729669 <\/p>\n
\u4e0d\u6253\u7b97\u7528\u4e8e\u4f7f\u7528\u683c\u5217\u9ad8\u91cc\u5386(1582)\u51fa\u73b0\u524d\u7684\u503c\u3002 <\/p>\n
\u7ed9\u51fa\u4e00\u4e2a\u5929\u6570N\uff0c\u8fd4\u56de\u4e00\u4e2aDATE\u503c\u3002
mysql> select FROM_DAYS(729669);
-> ‘1997-10-07’ <\/p>\n
\u6839\u636eformat\u5b57\u7b26\u4e32\u683c\u5f0f\u5316date\u503c\u3002\u4e0b\u5217\u4fee\u9970\u7b26\u53ef\u4ee5\u88ab\u7528\u5728format\u5b57\u7b26\u4e32\u4e2d\uff1a %M \u6708\u540d\u5b57(January\u2026\u2026December)
%W \u661f\u671f\u540d\u5b57(Sunday\u2026\u2026Saturday)
%D \u6709\u82f1\u8bed\u524d\u7f00\u7684\u6708\u4efd\u7684\u65e5\u671f(1st, 2nd, 3rd, \u7b49\u7b49\u3002\uff09
%Y \u5e74, \u6570\u5b57, 4 \u4f4d
%y \u5e74, \u6570\u5b57, 2 \u4f4d
%a \u7f29\u5199\u7684\u661f\u671f\u540d\u5b57(Sun\u2026\u2026Sat)
%d \u6708\u4efd\u4e2d\u7684\u5929\u6570, \u6570\u5b57(00\u2026\u202631)
%e \u6708\u4efd\u4e2d\u7684\u5929\u6570, \u6570\u5b57(0\u2026\u202631)
%m \u6708, \u6570\u5b57(01\u2026\u202612)
%c \u6708, \u6570\u5b57(1\u2026\u202612)
%b \u7f29\u5199\u7684\u6708\u4efd\u540d\u5b57(Jan\u2026\u2026Dec)
%j \u4e00\u5e74\u4e2d\u7684\u5929\u6570(001\u2026\u2026366)
%H \u5c0f\u65f6(00\u2026\u202623)
%k \u5c0f\u65f6(0\u2026\u202623)
%h \u5c0f\u65f6(01\u2026\u202612)
%I \u5c0f\u65f6(01\u2026\u202612)
%l \u5c0f\u65f6(1\u2026\u202612)
%i \u5206\u949f, \u6570\u5b57(00\u2026\u202659)
%r \u65f6\u95f4,12 \u5c0f\u65f6(hh:mm:ss [AP]M)
%T \u65f6\u95f4,24 \u5c0f\u65f6(hh:mm:ss)
%S \u79d2(00\u2026\u202659)
%s \u79d2(00\u2026\u202659)
%p AM\u6216PM
%w \u4e00\u4e2a\u661f\u671f\u4e2d\u7684\u5929\u6570(0=Sunday \u2026\u20266=Saturday \uff09
%U \u661f\u671f(0\u2026\u202652), \u8fd9\u91cc\u661f\u671f\u5929\u662f\u661f\u671f\u7684\u7b2c\u4e00\u5929
%u \u661f\u671f(0\u2026\u202652), \u8fd9\u91cc\u661f\u671f\u4e00\u662f\u661f\u671f\u7684\u7b2c\u4e00\u5929
%% \u4e00\u4e2a\u6587\u5b57“%”\u3002 <\/p>\n
-> ‘Saturday October 1997’
mysql> select DATE_FORMAT(‘1997-10-04 22:23:00’, ‘%H:%i:%s’);
-> ’22:23:00′
mysql> select DATE_FORMAT(‘1997-10-04 22:23:00’,
‘%D %y %a %d %m %b %j’);
-> ‘4th 97 Sat 04 10 Oct 277’
mysql> select DATE_FORMAT(‘1997-10-04 22:23:00’,
‘%H %k %I %r %T %S %w’);
-> ’22 22 10 10:23:00 PM 22:23:00 00 6′
MySQL3.23\u4e2d\uff0c\u5728\u683c\u5f0f\u4fee\u9970\u7b26\u5b57\u7b26\u524d\u9700\u8981%\u3002\u5728MySQL\u66f4\u65e9\u7684\u7248\u672c\u4e2d\uff0c%\u662f\u53ef\u9009\u7684\u3002 <\/p>\n
\u8fd9\u8c61\u4e0a\u9762\u7684DATE_FORMAT()\u51fd\u6570\u4e00\u6837\u4f7f\u7528\uff0c\u4f46\u662fformat\u5b57\u7b26\u4e32\u53ea\u80fd\u5305\u542b\u5904\u7406\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u7684\u90a3\u4e9b\u683c\u5f0f\u4fee\u9970\u7b26\u3002\u5176\u4ed6\u4fee\u9970\u7b26\u4ea7\u751f\u4e00\u4e2aNULL\u503c\u62160\u3002 <\/p>\n
\u3000
CURRENT_DATE
\u4ee5’YYYY-MM-DD’\u6216YYYYMMDD\u683c\u5f0f\u8fd4\u56de\u4eca\u5929\u65e5\u671f\u503c\uff0c\u53d6\u51b3\u4e8e\u51fd\u6570\u662f\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fd8\u662f\u6570\u5b57\u4e0a\u4e0b\u6587\u88ab\u4f7f\u7528\u3002
mysql> select CURDATE();
-> ‘1997-12-15’
mysql> select CURDATE() + 0;
-> 19971215 <\/p>\n
\u3000
CURRENT_TIME
\u4ee5’HH:MM:SS’\u6216HHMMSS\u683c\u5f0f\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u503c\uff0c\u53d6\u51b3\u4e8e\u51fd\u6570\u662f\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fd8\u662f\u5728\u6570\u5b57\u7684\u4e0a\u4e0b\u6587\u88ab\u4f7f\u7528\u3002
mysql> select CURTIME();
-> ’23:50:26′
mysql> select CURTIME() + 0;
-> 235026 <\/p>\n
\u3000
SYSDATE()<\/strong>
\u3000
CURRENT_TIMESTAMP
\u4ee5’YYYY-MM-DD HH:MM:SS’\u6216YYYYMMDDHHMMSS\u683c\u5f0f\u8fd4\u56de\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u53d6\u51b3\u4e8e\u51fd\u6570\u662f\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fd8\u662f\u5728\u6570\u5b57\u7684\u4e0a\u4e0b\u6587\u88ab\u4f7f\u7528\u3002
mysql> select NOW();
-> ‘1997-12-15 23:50:26’
mysql> select NOW() + 0;
-> 19971215235026 <\/p>\n
\u3000
UNIX_TIMESTAMP(date)
\u5982\u679c\u6ca1\u6709\u53c2\u6570\u8c03\u7528\uff0c\u8fd4\u56de\u4e00\u4e2aUnix\u65f6\u95f4\u6233\u8bb0(\u4ece’1970-01-01 00:00:00’GMT\u5f00\u59cb\u7684\u79d2\u6570)\u3002\u5982\u679cUNIX_TIMESTAMP()\u7528\u4e00\u4e2adate\u53c2\u6570\u88ab\u8c03\u7528\uff0c\u5b83\u8fd4\u56de\u4ece’1970-01-01 00:00:00′ GMT\u5f00\u59cb\u7684\u79d2\u6570\u503c\u3002date\u53ef\u4ee5\u662f\u4e00\u4e2aDATE\u5b57\u7b26\u4e32\u3001\u4e00\u4e2aDATETIME\u5b57\u7b26\u4e32\u3001\u4e00\u4e2aTIMESTAMP\u6216\u4ee5YYMMDD\u6216YYYYMMDD\u683c\u5f0f\u7684\u672c\u5730\u65f6\u95f4\u7684\u4e00\u4e2a\u6570\u5b57\u3002
mysql> select UNIX_TIMESTAMP();
-> 882226357
mysql> select UNIX_TIMESTAMP(‘1997-10-04 22:23:00’);
-> 875996580
\u5f53UNIX_TIMESTAMP\u88ab\u7528\u4e8e\u4e00\u4e2aTIMESTAMP\u5217\uff0c\u51fd\u6570\u5c06\u76f4\u63a5\u63a5\u53d7\u503c\uff0c\u6ca1\u6709\u9690\u542b\u7684“string-to-unix-timestamp”\u53d8\u6362\u3002 <\/p>\n
\u4ee5’YYYY-MM-DD HH:MM:SS’\u6216YYYYMMDDHHMMSS\u683c\u5f0f\u8fd4\u56deunix_timestamp\u53c2\u6570\u6240\u8868\u793a\u7684\u503c\uff0c\u53d6\u51b3\u4e8e\u51fd\u6570\u662f\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fd8\u662f\u6216\u6570\u5b57\u4e0a\u4e0b\u6587\u4e2d\u88ab\u4f7f\u7528\u3002
mysql> select FROM_UNIXTIME(875996580);
-> ‘1997-10-04 22:23:00’
mysql> select FROM_UNIXTIME(875996580) + 0;
-> 19971004222300 <\/p>\n
\u8fd4\u56de\u8868\u793a Unix \u65f6\u95f4\u6807\u8bb0\u7684\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6839\u636eformat\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3002format\u53ef\u4ee5\u5305\u542b\u4e0eDATE_FORMAT()\u51fd\u6570\u5217\u51fa\u7684\u6761\u76ee\u540c\u6837\u7684\u4fee\u9970\u7b26\u3002
mysql> select FROM_UNIXTIME(UNIX_TIMESTAMP(),
‘%Y %D %M %h:%i:%s %x’);
-> ‘1997 23rd December 03:43:30 x’ <\/p>\n
\u8fd4\u56deseconds\u53c2\u6570\uff0c\u53d8\u6362\u6210\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\uff0c\u503c\u4ee5’HH:MM:SS’\u6216HHMMSS\u683c\u5f0f\u5316\uff0c\u53d6\u51b3\u4e8e\u51fd\u6570\u662f\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fd8\u662f\u5728\u6570\u5b57\u4e0a\u4e0b\u6587\u4e2d\u88ab\u4f7f\u7528\u3002
mysql> select SEC_TO_TIME(2378);
-> ’00:39:38′
mysql> select SEC_TO_TIME(2378) + 0;
-> 3938 <\/p>\n
\u8fd4\u56detime\u53c2\u6570\uff0c\u8f6c\u6362\u6210\u79d2\u3002
mysql> select TIME_TO_SEC(’22:23:00′);
-> 80580
mysql> select TIME_TO_SEC(’00:39:38′);
-> 2378<\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"