{"id":94,"date":"2021-09-15T21:50:17","date_gmt":"2021-09-15T13:50:17","guid":{"rendered":"http:\/\/www.dataisfun.cn\/?p=94"},"modified":"2021-12-13T15:23:34","modified_gmt":"2021-12-13T07:23:34","slug":"presto-sql-%e6%97%a5%e6%9c%9f%e6%97%b6%e9%97%b4%e5%b8%b8%e7%94%a8%e7%9a%84%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.dataisfun.cn\/?p=94","title":{"rendered":"Presto SQL-\u65e5\u671f\u65f6\u95f4\u5e38\u7528\u7684\u5904\u7406"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">\u672c\u6587\u603b\u7ed3\u4e00\u4e0bPresto SQL\u65e5\u671f\u65f6\u95f4\u6bd4\u8f83\u5e38\u7528\u7684\u5904\u7406\uff1a<br><\/p>\n\n\n\n<h3 class=\"has-vivid-cyan-blue-color has-text-color wp-block-heading\" style=\"font-size:14px\"><strong>1. \u7279\u5b9a\u7684\u65e5\u671f\u65f6\u95f4\u503c<\/strong><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">select\n\n    current_date, --\u5f53\u524d\u7684\u65e5\u671f\n    \n    current_time, --\u5f53\u524d\u7684\u65f6\u95f4\n    \n    current_timestamp, --\u5f53\u524d\u7684\u65f6\u95f4\u6233\n    \n    date'2021-09-01', --\u7ed9\u5b9a\u7684\u65e5\u671f\n    \n    time'12:30:00', --\u7ed9\u5b9a\u7684\u65f6\u95f4\n    \n    timestamp'2021-09-01 12:30:00' --\u7ed9\u5b9a\u7684\u65f6\u95f4\u6233<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">\u9700\u8981\u8f93\u5165\u65e5\u671f\u65f6\u95f4\u65f6\uff0c\u4f7f\u7528\u524d\u7f00date\u3001timestamp\u3001time\u5c31\u4f1a\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u8bc6\u522b\u4e3a\u65e5\u671f\u65f6\u95f4\u3002<br><br><\/p>\n\n\n\n<h3 class=\"has-vivid-cyan-blue-color has-text-color wp-block-heading\" style=\"font-size:14px\">2. \u5e38\u7528\u7684\u65e5\u671f\u65f6\u95f4\u5904\u7406<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">select\n\n    current_date, --\u5f53\u524d\u65e5\u671f\n    \n    current_date - interval '1' day, --\u5f80\u524d\u63a81\u5929\n    \n    current_date + interval '3' day, --\u5f80\u540e\u63a83\u5929\n    \n    date_add('day', -1, current_date), --\u5f80\u524d\u63a81\u5929\n    \n    date_add('day', 3, current_date), --\u5f80\u540e\u63a83\u5929\n    \n    date_trunc('month', current_date), --\u5f53\u6708\u7b2c1\u5929\n    \n    date_trunc('month', current_date) - interval '1' month, --\u524d1\u6708\u7b2c1\u5929\n    \n    date_trunc('month', current_date) - interval '1' day, --\u524d1\u6708\u6700\u540e1\u5929\n    \n    date_trunc('week', current_date), --\u5f53\u5468\u7b2c1\u5929\n    \n    date_diff('day', date'2021-09-01', date'2021-09-23'), --\u76f8\u5dee\u5929\u6570\n    \n    to_char(current_date,\u00a0'yyyy-mm'),\u00a0--\u5c06\u65e5\u671f\u65f6\u95f4\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6708\u4efd\n    \n    to_char(current_date, 'yyyymmdd'), --\u5c06\u65e5\u671f\u65f6\u95f4\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u65e5\u671f\n    \n    date(date_parse('20210901', '%Y%m%d')), --\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u503c\n    \n    date('2021-09-01'), --\u5c06\u65e5\u671f\u5b57\u7b26\u4e32yyyy-mm-dd\u8f6c\u6362\u4e3a\u65e5\u671f\u503c\n    \n    cast('2021-09-01' as date) --\u5c06\u65e5\u671f\u5b57\u7b26\u4e32yyyy-mm-dd\u8f6c\u6362\u4e3a\u65e5\u671f\u503c<\/pre>\n\n\n\n<h3 class=\"has-vivid-cyan-blue-color has-text-color wp-block-heading\" style=\"font-size:12px\">date_trunc(unit, x) \u2192 [same as input]<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:10px\">\u5c06\u65e5\u671f\u65f6\u95f4\u503c\u6309\u7167\u7ed9\u5b9a\u7684\u5355\u4f4d\u8fdb\u884c\u8f6c\u6362\uff0c\u8f93\u51fa\u7684\u5b57\u6bb5\u7c7b\u578b\u548c\u8f93\u5165\u7684\u5b57\u6bb5\u7c7b\u578b\u662f\u4e00\u6837\u7684\u3002<\/p>\n\n\n\n<h3 class=\"has-vivid-cyan-blue-color has-text-color wp-block-heading\" style=\"font-size:12px\">date_add(unit, value, timestamp) \u2192 [same as input]<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:10px\">\u5c06\u65e5\u671f\u65f6\u95f4\u503c\u6309\u7167\u7ed9\u5b9a\u7684\u5355\u4f4d\u548c\u6570\u503c\u8fdb\u884c\u52a0\u51cf\uff0c \u8f93\u51fa\u7684\u5b57\u6bb5\u7c7b\u578b\u548c\u8f93\u5165\u7684\u5b57\u6bb5\u7c7b\u578b\u662f\u4e00\u6837\u7684 \u3002<\/p>\n\n\n\n<h3 class=\"has-vivid-cyan-blue-color has-text-color wp-block-heading\" style=\"font-size:12px\">date_diff(unit, timestamp1, timestamp2) \u2192 bigint<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:10px\">\u8ba1\u7b972\u4e2a\u65e5\u671f\u65f6\u95f4\u76f8\u5dee\u591a\u5c11\u6570\u91cf\u7684\u6307\u5b9a\u5355\u4f4d\uff0c\u8f93\u51fa\u7684\u5b57\u6bb5\u7c7b\u578b\u4e3a bigint\u3002<\/p>\n\n\n\n<h3 class=\"has-vivid-cyan-blue-color has-text-color wp-block-heading\" style=\"font-size:12px\">date_parse(string, format) \u2192 timestamp<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:10px\">\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u65e5\u671f\u65f6\u95f4\u503c\uff0c\u8f93\u51fa\u7684\u5b57\u6bb5\u7c7b\u578b\u4e3a timestamp\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\"><br><strong>date_trunc()\u3001date_add()\u3001date_diff()\u7b49\u65e5\u671f\u65f6\u95f4\u51fd\u6570\u7684 unit \u53c2\u6570\u53ef\u4ee5\u53d6\uff1asecond \u79d2,&nbsp; minute \u5206,&nbsp; hour \u5c0f\u65f6,&nbsp; day \u65e5,&nbsp; week \u5468,&nbsp; month \u6708,&nbsp; quarter \u5b63\u5ea6,&nbsp; year \u5e74\uff0c\u5176\u4e2d hour&nbsp; day&nbsp; week&nbsp; month \u6bd4\u8f83\u5e38\u7528\u3002<\/strong><br><br><\/p>\n\n\n\n<h3 class=\"has-vivid-cyan-blue-color has-text-color wp-block-heading\" style=\"font-size:14px\">3. \u5076\u5c14\u7528\u7684\u65e5\u671f\u65f6\u95f4\u5904\u7406<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\">\u5176\u4ed6\u5076\u5c14\u4f1a\u7528\u5230\u7684\u8fd8\u6709\uff1ahour()\u3001day()\u3001week()\u3001month()\u3001year()\u3001day_of_week() \u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:12px\"><br> <span class=\"sme-text-color has-cyan-bluish-gray-color\">\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u8f6c\u8f7d\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u5e76\u7559\u4e0b\u539f\u6587\u94fe\u63a5\u3002<\/span> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u603b\u7ed3\u4e00\u4e0bPresto SQL\u65e5\u671f\u65f6\u95f4\u6bd4\u8f83\u5e38\u7528\u7684\u5904\u7406\uff1a 1. \u7279\u5b9a\u7684\u65e5\u671f\u65f6\u95f4\u503c \u9700\u8981\u8f93\u5165\u65e5\u671f\u65f6\u95f4\u65f6&hellip; <a href=\"https:\/\/www.dataisfun.cn\/?p=94\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">Presto SQL-\u65e5\u671f\u65f6\u95f4\u5e38\u7528\u7684\u5904\u7406<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[9,6],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-sql","tag-presto","tag-sql","h-entry","h-as-article"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.11 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Presto SQL \u65e5\u671f\u65f6\u95f4 | Presto SQL \u65e5\u671f\u65f6\u95f4\u5904\u7406 - \u6570\u636e\u597d\u73a9<\/title>\n<meta name=\"description\" content=\"\u672c\u6587\u603b\u7ed3\u4e00\u4e0bPresto SQL\u65e5\u671f\u65f6\u95f4\u6bd4\u8f83\u5e38\u7528\u7684\u5904\u7406\uff0cdate_add(), date_diff(), date_trunc(), date_parse()\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dataisfun.cn\/?p=94\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Presto SQL \u65e5\u671f\u65f6\u95f4 | Presto SQL \u65e5\u671f\u65f6\u95f4\u5904\u7406 - \u6570\u636e\u597d\u73a9\" \/>\n<meta property=\"og:description\" content=\"\u672c\u6587\u603b\u7ed3\u4e00\u4e0bPresto SQL\u65e5\u671f\u65f6\u95f4\u6bd4\u8f83\u5e38\u7528\u7684\u5904\u7406\uff0cdate_add(), date_diff(), date_trunc(), date_parse()\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dataisfun.cn\/?p=94\" \/>\n<meta property=\"og:site_name\" content=\"\u6570\u636e\u597d\u73a9\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-15T13:50:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-13T07:23:34+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dataisfun.cn\/?p=94\",\"url\":\"https:\/\/www.dataisfun.cn\/?p=94\",\"name\":\"Presto SQL \u65e5\u671f\u65f6\u95f4 | Presto SQL \u65e5\u671f\u65f6\u95f4\u5904\u7406 - \u6570\u636e\u597d\u73a9\",\"isPartOf\":{\"@id\":\"https:\/\/www.dataisfun.cn\/#website\"},\"datePublished\":\"2021-09-15T13:50:17+00:00\",\"dateModified\":\"2021-12-13T07:23:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.dataisfun.cn\/#\/schema\/person\/026bc03694bbf1608ccc639fb31c3293\"},\"description\":\"\u672c\u6587\u603b\u7ed3\u4e00\u4e0bPresto SQL\u65e5\u671f\u65f6\u95f4\u6bd4\u8f83\u5e38\u7528\u7684\u5904\u7406\uff0cdate_add(), date_diff(), date_trunc(), date_parse()\",\"breadcrumb\":{\"@id\":\"https:\/\/www.dataisfun.cn\/?p=94#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dataisfun.cn\/?p=94\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dataisfun.cn\/?p=94#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.dataisfun.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Presto SQL-\u65e5\u671f\u65f6\u95f4\u5e38\u7528\u7684\u5904\u7406\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dataisfun.cn\/#website\",\"url\":\"https:\/\/www.dataisfun.cn\/\",\"name\":\"\u6570\u636e\u597d\u73a9\",\"description\":\"\u6570\u4e2d\u81ea\u6709\u4e7e\u5764\uff0c\u4e8e\u4e50\u8da3\u4e2d\u53d1\u73b0\u4ef7\u503c\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dataisfun.cn\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dataisfun.cn\/#\/schema\/person\/026bc03694bbf1608ccc639fb31c3293\",\"name\":\"admin\",\"sameAs\":[\"http:\/\/www.dataisfun.cn\"],\"url\":\"https:\/\/www.dataisfun.cn\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Presto SQL \u65e5\u671f\u65f6\u95f4 | Presto SQL \u65e5\u671f\u65f6\u95f4\u5904\u7406 - \u6570\u636e\u597d\u73a9","description":"\u672c\u6587\u603b\u7ed3\u4e00\u4e0bPresto SQL\u65e5\u671f\u65f6\u95f4\u6bd4\u8f83\u5e38\u7528\u7684\u5904\u7406\uff0cdate_add(), date_diff(), date_trunc(), date_parse()","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dataisfun.cn\/?p=94","og_locale":"zh_CN","og_type":"article","og_title":"Presto SQL \u65e5\u671f\u65f6\u95f4 | Presto SQL \u65e5\u671f\u65f6\u95f4\u5904\u7406 - \u6570\u636e\u597d\u73a9","og_description":"\u672c\u6587\u603b\u7ed3\u4e00\u4e0bPresto SQL\u65e5\u671f\u65f6\u95f4\u6bd4\u8f83\u5e38\u7528\u7684\u5904\u7406\uff0cdate_add(), date_diff(), date_trunc(), date_parse()","og_url":"https:\/\/www.dataisfun.cn\/?p=94","og_site_name":"\u6570\u636e\u597d\u73a9","article_published_time":"2021-09-15T13:50:17+00:00","article_modified_time":"2021-12-13T07:23:34+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.dataisfun.cn\/?p=94","url":"https:\/\/www.dataisfun.cn\/?p=94","name":"Presto SQL \u65e5\u671f\u65f6\u95f4 | Presto SQL \u65e5\u671f\u65f6\u95f4\u5904\u7406 - \u6570\u636e\u597d\u73a9","isPartOf":{"@id":"https:\/\/www.dataisfun.cn\/#website"},"datePublished":"2021-09-15T13:50:17+00:00","dateModified":"2021-12-13T07:23:34+00:00","author":{"@id":"https:\/\/www.dataisfun.cn\/#\/schema\/person\/026bc03694bbf1608ccc639fb31c3293"},"description":"\u672c\u6587\u603b\u7ed3\u4e00\u4e0bPresto SQL\u65e5\u671f\u65f6\u95f4\u6bd4\u8f83\u5e38\u7528\u7684\u5904\u7406\uff0cdate_add(), date_diff(), date_trunc(), date_parse()","breadcrumb":{"@id":"https:\/\/www.dataisfun.cn\/?p=94#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dataisfun.cn\/?p=94"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dataisfun.cn\/?p=94#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.dataisfun.cn\/"},{"@type":"ListItem","position":2,"name":"Presto SQL-\u65e5\u671f\u65f6\u95f4\u5e38\u7528\u7684\u5904\u7406"}]},{"@type":"WebSite","@id":"https:\/\/www.dataisfun.cn\/#website","url":"https:\/\/www.dataisfun.cn\/","name":"\u6570\u636e\u597d\u73a9","description":"\u6570\u4e2d\u81ea\u6709\u4e7e\u5764\uff0c\u4e8e\u4e50\u8da3\u4e2d\u53d1\u73b0\u4ef7\u503c","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dataisfun.cn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.dataisfun.cn\/#\/schema\/person\/026bc03694bbf1608ccc639fb31c3293","name":"admin","sameAs":["http:\/\/www.dataisfun.cn"],"url":"https:\/\/www.dataisfun.cn\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/www.dataisfun.cn\/index.php?rest_route=\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dataisfun.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dataisfun.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dataisfun.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dataisfun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=94"}],"version-history":[{"count":22,"href":"https:\/\/www.dataisfun.cn\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":767,"href":"https:\/\/www.dataisfun.cn\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions\/767"}],"wp:attachment":[{"href":"https:\/\/www.dataisfun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dataisfun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dataisfun.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}