{"id":1970,"date":"2023-10-28T21:36:46","date_gmt":"2023-10-28T21:36:46","guid":{"rendered":"https:\/\/feellikelearning.com\/?p=1970"},"modified":"2023-11-02T18:11:22","modified_gmt":"2023-11-02T18:11:22","slug":"postgresql-note","status":"publish","type":"post","link":"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/","title":{"rendered":"PostgreSQL \u7b14\u8bb0"},"content":{"rendered":"\n<h2>\u628a\u4e00\u4e2a\u8868\u7684\u6570\u636e\u52a0\u5230\u53e6\u4e00\u8868<\/h2>\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=\"\">INSERT INTO my_table (col1, col2)\nSELECT col1, col2\nFROM my_table2;\n<\/pre>\n\n\n\n<h2>\u4ecetimestamp\u91cc\u9762\u63d0\u53d6Date<\/h2>\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=\"\">-- method 1\nSELECT DATE(your_timestamp_column) AS extracted_date\nFROM your_table;\n\n-- method 2\nSELECT EXTRACT(DATE FROM your_timestamp_column) AS extracted_date\nFROM your_table;\n<\/pre>\n\n\n\n<p>\u7ed9\u4e00\u4e2a\u8868\u6dfb\u52a0\u5217<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- pattern\nALTER TABLE table_name\nADD COLUMN new_column_name data_type;\n\n-- example 1\nALTER TABLE employees\nADD COLUMN email VARCHAR(255);\n\n-- example 2\nALTER TABLE employees\nADD COLUMN email VARCHAR(255) NOT NULL DEFAULT 'N\/A';\n\n-- example 3\nALTER TABLE table_name\nADD COLUMN new_column_name TEXT;\n\n<\/pre>\n\n\n\n<h2>\u7528with keyword\u6267\u884c\u4e00\u7cfb\u5217\u67e5\u8be2<\/h2>\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=\"\">-- Create multiple CTEs and then select from them\nWITH cte1 AS (\n    SELECT\n        id,\n        name\n    FROM\n        table1\n),\ncte2 AS (\n    SELECT\n        id,\n        description\n    FROM\n        table2\n)\n-- Select from the CTEs\nSELECT\n    cte1.id AS cte1_id,\n    cte1.name,\n    cte2.id AS cte2_id,\n    cte2.description\nFROM\n    cte1\nJOIN\n    cte2 ON cte1.id = cte2.id;\n<\/pre>\n\n\n\n<h2>\u7528\u4e34\u65f6\u8868<\/h2>\n\n\n\n<p>WITH&#8230;AS&#8230; \u7684\u8868\u5728query\u5b8c\u6210\u540e\u5c31\u6d88\u5931\u4e86\uff0c\u5982\u679c\u5728\u5f53\u524dsession\u9700\u8981\u7684\u4e34\u65f6\u8868\uff0c\u53ef\u4ee5\u7528temp table\u3002\u8bed\u6cd5\u548c\u666e\u901a\u8868\u51e0\u4e4e\u4e00\u6837\uff0c\u5c31\u662f\u591a\u4e86\u4e2aTEMP\u5173\u952e\u8bcd\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Create a temporary table named \"temp_sales\" with some sample data\nCREATE TEMP TABLE temp_sales (\n    order_id serial PRIMARY KEY,\n    product_name VARCHAR(255),\n    quantity INT,\n    order_date DATE\n);\n\n-- Insert some sample data into the temporary table\nINSERT INTO temp_sales (product_name, quantity, order_date)\nVALUES\n    ('Product A', 10, '2023-10-16'),\n    ('Product B', 5, '2023-10-17'),\n    ('Product C', 8, '2023-10-18');\n\n-- Query the temporary table\nSELECT * FROM temp_sales;\n\n-- Drop the temporary table (Optional, it will be dropped automatically at the end of the session)\n-- DROP TABLE temp_sales;<\/pre>\n\n\n\n<p>temp table\u548c\u666e\u901a\u8868\u7684\u533a\u522b\u662f\uff0ctemp table\u53ea\u5728\u5f53\u524dsession\u5b58\u5728\uff0csession\u7ed3\u675f\u540e\u5c31\u4f1a\u81ea\u52a8\u6e05\u7a7a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u628a\u4e00\u4e2a\u8868\u7684\u6570\u636e\u52a0\u5230\u53e6\u4e00\u8868 \u4ecetimestamp\u91cc\u9762\u63d0\u53d6Date \u7ed9\u4e00\u4e2a\u8868\u6dfb\u52a0\u5217 \u7528with keyword\u6267\u884c\u4e00\u7cfb\u5217\u67e5\u8be2 \u7528\u4e34\u65f6\u8868 WITH&#8230;AS&#8230; \u7684\u8868\u5728query\u5b8c\u6210\u540e\u5c31\u6d88\u5931\u4e86\uff0c\u5982\u679c\u5728\u5f53\u524dsession\u9700\u8981\u7684\u4e34\u65f6\u8868\uff0c\u53ef\u4ee5\u7528temp table\u3002\u8bed\u6cd5\u548c\u666e\u901a\u8868\u51e0\u4e4e\u4e00\u6837\uff0c\u5c31\u662f\u591a\u4e86\u4e2aTEMP\u5173\u952e\u8bcd\u3002 temp table\u548c\u666e\u901a\u8868\u7684\u533a\u522b\u662f\uff0ctemp table\u53ea\u5728\u5f53\u524dsession\u5b58\u5728\uff0csession\u7ed3\u675f\u540e\u5c31\u4f1a\u81ea\u52a8\u6e05\u7a7a\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PostgreSQL \u7b14\u8bb0 | Feel Like Learning<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL \u7b14\u8bb0 | Feel Like Learning\" \/>\n<meta property=\"og:description\" content=\"\u628a\u4e00\u4e2a\u8868\u7684\u6570\u636e\u52a0\u5230\u53e6\u4e00\u8868 \u4ecetimestamp\u91cc\u9762\u63d0\u53d6Date \u7ed9\u4e00\u4e2a\u8868\u6dfb\u52a0\u5217 \u7528with keyword\u6267\u884c\u4e00\u7cfb\u5217\u67e5\u8be2 \u7528\u4e34\u65f6\u8868 WITH&#8230;AS&#8230; \u7684\u8868\u5728query\u5b8c\u6210\u540e\u5c31\u6d88\u5931\u4e86\uff0c\u5982\u679c\u5728\u5f53\u524dsession\u9700\u8981\u7684\u4e34\u65f6\u8868\uff0c\u53ef\u4ee5\u7528temp table\u3002\u8bed\u6cd5\u548c\u666e\u901a\u8868\u51e0\u4e4e\u4e00\u6837\uff0c\u5c31\u662f\u591a\u4e86\u4e2aTEMP\u5173\u952e\u8bcd\u3002 temp table\u548c\u666e\u901a\u8868\u7684\u533a\u522b\u662f\uff0ctemp table\u53ea\u5728\u5f53\u524dsession\u5b58\u5728\uff0csession\u7ed3\u675f\u540e\u5c31\u4f1a\u81ea\u52a8\u6e05\u7a7a\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/\" \/>\n<meta property=\"og:site_name\" content=\"Feel Like Learning\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-28T21:36:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-02T18:11:22+00:00\" \/>\n<meta name=\"author\" content=\"feellikelearning\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"feellikelearning\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/\"},\"author\":{\"name\":\"feellikelearning\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"headline\":\"PostgreSQL \u7b14\u8bb0\",\"datePublished\":\"2023-10-28T21:36:46+00:00\",\"dateModified\":\"2023-11-02T18:11:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/\"},\"wordCount\":18,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/\",\"url\":\"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/\",\"name\":\"PostgreSQL \u7b14\u8bb0 | Feel Like Learning\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/#website\"},\"datePublished\":\"2023-10-28T21:36:46+00:00\",\"dateModified\":\"2023-11-02T18:11:22+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/feellikelearning.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL \u7b14\u8bb0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/feellikelearning.com\/#website\",\"url\":\"https:\/\/feellikelearning.com\/\",\"name\":\"Feel Like Learning\",\"description\":\"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230\",\"publisher\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/feellikelearning.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a\",\"name\":\"feellikelearning\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g\",\"caption\":\"feellikelearning\"},\"logo\":{\"@id\":\"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL \u7b14\u8bb0 | Feel Like Learning","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:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL \u7b14\u8bb0 | Feel Like Learning","og_description":"\u628a\u4e00\u4e2a\u8868\u7684\u6570\u636e\u52a0\u5230\u53e6\u4e00\u8868 \u4ecetimestamp\u91cc\u9762\u63d0\u53d6Date \u7ed9\u4e00\u4e2a\u8868\u6dfb\u52a0\u5217 \u7528with keyword\u6267\u884c\u4e00\u7cfb\u5217\u67e5\u8be2 \u7528\u4e34\u65f6\u8868 WITH&#8230;AS&#8230; \u7684\u8868\u5728query\u5b8c\u6210\u540e\u5c31\u6d88\u5931\u4e86\uff0c\u5982\u679c\u5728\u5f53\u524dsession\u9700\u8981\u7684\u4e34\u65f6\u8868\uff0c\u53ef\u4ee5\u7528temp table\u3002\u8bed\u6cd5\u548c\u666e\u901a\u8868\u51e0\u4e4e\u4e00\u6837\uff0c\u5c31\u662f\u591a\u4e86\u4e2aTEMP\u5173\u952e\u8bcd\u3002 temp table\u548c\u666e\u901a\u8868\u7684\u533a\u522b\u662f\uff0ctemp table\u53ea\u5728\u5f53\u524dsession\u5b58\u5728\uff0csession\u7ed3\u675f\u540e\u5c31\u4f1a\u81ea\u52a8\u6e05\u7a7a\u3002","og_url":"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/","og_site_name":"Feel Like Learning","article_published_time":"2023-10-28T21:36:46+00:00","article_modified_time":"2023-11-02T18:11:22+00:00","author":"feellikelearning","twitter_card":"summary_large_image","twitter_misc":{"Written by":"feellikelearning","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/#article","isPartOf":{"@id":"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/"},"author":{"name":"feellikelearning","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"headline":"PostgreSQL \u7b14\u8bb0","datePublished":"2023-10-28T21:36:46+00:00","dateModified":"2023-11-02T18:11:22+00:00","mainEntityOfPage":{"@id":"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/"},"wordCount":18,"commentCount":0,"publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/","url":"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/","name":"PostgreSQL \u7b14\u8bb0 | Feel Like Learning","isPartOf":{"@id":"https:\/\/feellikelearning.com\/#website"},"datePublished":"2023-10-28T21:36:46+00:00","dateModified":"2023-11-02T18:11:22+00:00","breadcrumb":{"@id":"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/feellikelearning.com\/index.php\/2023\/10\/28\/postgresql-note\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/feellikelearning.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL \u7b14\u8bb0"}]},{"@type":"WebSite","@id":"https:\/\/feellikelearning.com\/#website","url":"https:\/\/feellikelearning.com\/","name":"Feel Like Learning","description":"\u7a0b\u5e8f\uff5c\u751f\u6d3b\uff5c\u5b66\u5230\u5c31\u662f\u8d5a\u5230","publisher":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/feellikelearning.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/91fb815bebebf166c217b5e3764d437a","name":"feellikelearning","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/72a1e86e9dcb0332e88bd7d54fd36c28?s=96&d=mm&r=g","caption":"feellikelearning"},"logo":{"@id":"https:\/\/feellikelearning.com\/#\/schema\/person\/image\/"},"url":"https:\/\/feellikelearning.com\/index.php\/author\/feellikelearning\/"}]}},"_links":{"self":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/1970"}],"collection":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/comments?post=1970"}],"version-history":[{"count":8,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/1970\/revisions"}],"predecessor-version":[{"id":1983,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/posts\/1970\/revisions\/1983"}],"wp:attachment":[{"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/media?parent=1970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/categories?post=1970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feellikelearning.com\/index.php\/wp-json\/wp\/v2\/tags?post=1970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}