{"id":637,"date":"2025-12-23T12:29:24","date_gmt":"2025-12-23T04:29:24","guid":{"rendered":"https:\/\/book.yuekegu.com\/?p=637"},"modified":"2025-12-23T12:29:25","modified_gmt":"2025-12-23T04:29:25","slug":"timestamp%e8%bd%ac%e4%b8%badatetime%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/book.yuekegu.com\/index.php\/2025\/12\/23\/timestamp%e8%bd%ac%e4%b8%badatetime%e6%a0%bc%e5%bc%8f\/","title":{"rendered":"TIMESTAMP\u8f6c\u4e3adatetime\u683c\u5f0f"},"content":{"rendered":"\n<p>UPDATE <code>sq_question_content<\/code> as c<br>INNER JOIN sq_question as s ON c.question_id = s.id<br>SET<br>c.create_at = FROM_UNIXTIME(s.pubdate) ,<br>c.update_at = FROM_UNIXTIME(s.pubdate);<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u9519\u8bef\u7684\u6838\u5fc3\u539f\u56e0\u662f\uff1a<strong><code>sq_question.pubdate<\/code> \u5e76\u975e MySQL \u539f\u751f\u7684 <code>TIMESTAMP<\/code> \u7c7b\u578b<\/strong>\uff0c\u800c\u662f\u4ee5<strong>\u6574\u6570\uff08Unix \u65f6\u95f4\u6233\uff0c\u79d2\u6570\uff09<\/strong>\u5f62\u5f0f\u5b58\u50a8\u7684\u6570\u503c\uff08\u5982 <code>1459849198<\/code> \u662f 2016\u5e744\u67085\u65e5\u7684 Unix \u65f6\u95f4\u6233\uff09\uff0c\u76f4\u63a5\u7528 <code>CAST(...) AS DATETIME<\/code> \u65e0\u6cd5\u5c06\u6570\u5b57\u8f6c\u4e3a\u5408\u6cd5\u7684 datetime \u683c\u5f0f\uff0c\u56e0\u6b64\u89e6\u53d1 <code>1292<\/code> \u9519\u8bef\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528 <code>FROM_UNIXTIME()<\/code> \u51fd\u6570\u8f6c\u6362 Unix \u65f6\u95f4\u6233<\/h3>\n\n\n\n<p>MySQL \u63d0\u4f9b\u4e86 <code>FROM_UNIXTIME(unix_timestamp)<\/code> \u51fd\u6570\uff0c\u53ef\u5c06<strong>\u79d2\u7ea7 Unix \u65f6\u95f4\u6233<\/strong>\u76f4\u63a5\u8f6c\u6362\u4e3a <code>DATETIME<\/code> \u7c7b\u578b\uff0c\u66ff\u6362\u539f\u6765\u7684 <code>CAST<\/code> \u5373\u53ef\u89e3\u51b3\u95ee\u9898\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE `sq_question_content` AS c\nINNER JOIN sq_question AS s ON c.question_id = s.id\nSET \n    c.create_at = FROM_UNIXTIME(s.pubdate),\n    c.update_at = FROM_UNIXTIME(s.pubdate);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u989d\u5916\u8bf4\u660e\u4e0e\u9a8c\u8bc1\u6b65\u9aa4<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u5148\u9a8c\u8bc1\u65f6\u95f4\u6233\u8f6c\u6362\u7ed3\u679c\uff08\u907f\u514d\u8bef\u64cd\u4f5c\uff09<\/h4>\n\n\n\n<p>\u6267\u884c\u66f4\u65b0\u524d\uff0c\u5148\u901a\u8fc7 <code>SELECT<\/code> \u786e\u8ba4\u8f6c\u6362\u540e\u7684\u65f6\u95f4\u662f\u5426\u6b63\u786e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    s.id,\n    s.pubdate AS unix_timestamp, -- \u539f\u59cbUnix\u65f6\u95f4\u6233\n    FROM_UNIXTIME(s.pubdate) AS converted_datetime -- \u8f6c\u6362\u540e\u7684datetime\nFROM sq_question AS s\nLIMIT 10; -- \u67e5\u770b\u524d10\u6761\u6570\u636e\u7684\u8f6c\u6362\u7ed3\u679c<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u5904\u7406<strong>\u6beb\u79d2\u7ea7 Unix \u65f6\u95f4\u6233<\/strong>\uff08\u82e5\u5b58\u5728\uff09<\/h4>\n\n\n\n<p>\u5982\u679c <code>pubdate<\/code> \u662f<strong>13\u4f4d\u7684\u6beb\u79d2\u7ea7\u65f6\u95f4\u6233<\/strong>\uff08\u5982 <code>1459849198123<\/code>\uff09\uff0c\u76f4\u63a5\u7528 <code>FROM_UNIXTIME()<\/code> \u4f1a\u5f97\u5230\u9519\u8bef\u65f6\u95f4\uff0c\u9700\u5148\u9664\u4ee51000\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE `sq_question_content` AS c\nINNER JOIN sq_question AS s ON c.question_id = s.id\nSET \n    c.create_at = FROM_UNIXTIME(s.pubdate \/ 1000),\n    c.update_at = FROM_UNIXTIME(s.pubdate \/ 1000);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u8fc7\u6ee4\u65e0\u6548\u65f6\u95f4\u6233\uff08\u53ef\u9009\uff09<\/h4>\n\n\n\n<p>\u82e5\u5b58\u5728\u975e\u6cd5\u7684\u65f6\u95f4\u6233\u503c\uff08\u59820\u3001\u8d1f\u6570\u3001\u8d85\u51fadatetime\u8303\u56f4\u7684\u6570\u503c\uff09\uff0c\u53ef\u901a\u8fc7 <code>WHERE<\/code> \u6761\u4ef6\u8fc7\u6ee4\uff0c\u907f\u514d\u518d\u6b21\u62a5\u9519\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE `sq_question_content` AS c\nINNER JOIN sq_question AS s ON c.question_id = s.id\nSET \n    c.create_at = FROM_UNIXTIME(s.pubdate),\n    c.update_at = FROM_UNIXTIME(s.pubdate)\nWHERE \n    s.pubdate &gt; 0 -- \u6392\u96640\u6216\u8d1f\u6570\n    AND s.pubdate &lt; FROM_UNIXTIME('2038-01-19 03:14:07') -- \u6392\u9664\u8d85\u51faUnix\u65f6\u95f4\u6233\u4e0a\u9650\u7684\u503c\n    AND FROM_UNIXTIME(s.pubdate) IS NOT NULL; -- \u786e\u4fdd\u8f6c\u6362\u7ed3\u679c\u6709\u6548<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4. \u786e\u8ba4\u76ee\u6807\u5b57\u6bb5\u7c7b\u578b<\/h4>\n\n\n\n<p>\u786e\u4fdd <code>sq_question_content.create_at<\/code> \u548c <code>update_at<\/code> \u662f <code>DATETIME<\/code>\/<code>DATETIME(6)<\/code> \u7c7b\u578b\uff0c\u82e5\u4e3a\u5176\u4ed6\u7c7b\u578b\uff08\u5982 <code>VARCHAR<\/code>\uff09\uff0c\u9700\u5148\u4fee\u6539\u5b57\u6bb5\u7c7b\u578b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE sq_question_content\nMODIFY COLUMN create_at DATETIME NOT NULL,\nMODIFY COLUMN update_at DATETIME NOT NULL;<\/code><\/pre>\n\n\n\n \n","protected":false},"excerpt":{"rendered":"<p>UPDATE sq_question_content as cINNER JOIN sq_question as s ON c.question_id = s.idSETc.create_at = FROM_UNIXTIME(s.pubdate) ,c.update_at = FROM_UNIXTIME(s.pubdate); \u8fd9\u4e2a\u9519\u8bef\u7684\u6838\u5fc3\u539f\u56e0\u662f\uff1asq_question.pubdate \u5e76\u975e MySQL \u539f\u751f\u7684 TIMESTAMP \u7c7b\u578b\uff0c\u800c\u662f\u4ee5\u6574\u6570\uff08Unix \u65f6\u95f4\u6233\uff0c\u79d2\u6570\uff09\u5f62\u5f0f\u5b58\u50a8\u7684\u6570\u503c\uff08\u5982 1459849198 \u662f 2016\u5e744\u67085\u65e5\u7684 Unix \u65f6\u95f4\u6233\uff09\uff0c\u76f4\u63a5\u7528 CAST(&#8230;) AS DATETIME \u65e0\u6cd5\u5c06\u6570\u5b57\u8f6c\u4e3a\u5408\u6cd5\u7684 datetime \u683c\u5f0f\uff0c\u56e0\u6b64\u89e6\u53d1 1292 \u9519\u8bef\u3002 \u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528 FROM_UNIXTIME() \u51fd\u6570\u8f6c\u6362 Unix \u65f6\u95f4\u6233 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-637","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/comments?post=637"}],"version-history":[{"count":1,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/637\/revisions"}],"predecessor-version":[{"id":638,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/637\/revisions\/638"}],"wp:attachment":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/media?parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/categories?post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/tags?post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}