{"id":5421,"date":"2023-03-23T20:52:11","date_gmt":"2023-03-23T12:52:11","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5421"},"modified":"2023-03-23T20:52:11","modified_gmt":"2023-03-23T12:52:11","slug":"postgresql%e7%9a%84%e6%9f%a5%e8%af%a2%e8%af%ad%e6%b3%95%e7%ae%80%e5%8d%95%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5421.html","title":{"rendered":"PostgreSQL\u7684\u67e5\u8be2\u8bed\u6cd5\u7b80\u5355\u5b66\u4e60"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u5f88\u65e9\u5c31\u542c\u8bf4\u8fc7 PostgreSQL \u8fd9\u4e2a\u6570\u636e\u5e93\uff0c\u8fd1\u671f\u7ffb\u9605blog\u7684\u65f6\u5019\u53d1\u73b014\/15\u5e74\u7684\u65f6\u5019\u4e5f\u7b80\u5355\u8bb0\u5f55\u8fc7\u4e00\u4e9b PostgreSQL \u7684\u77e5\u8bc6\u70b9\uff0c\u4f46\u4e3b\u8981\u90fd\u662f\u5b89\u88c5\u548c\u67e5\u770b\u4fe1\u606f\u7684\u547d\u4ee4\u7684\u603b\u7ed3\uff0c\u5177\u4f53\u7684\u4e00\u4e9b\u67e5\u8be2\u8bed\u6cd5\u6ca1\u600e\u4e48\u6d89\u53ca\u5230\uff0c\u8fd1\u671f\u56e0\u4e3a\u5de5\u4f5c\u4e0a\u7684\u539f\u56e0\u63a5\u89e6\u4e86\u4e00\u4e0b\uff0c\u867d\u7136\u5927\u90e8\u5206\u8bed\u6cd5\u548c\u529f\u80fd\u540c MySQL\/Hive \u7684SQL\u8bed\u6cd5\u4e00\u6837\uff0c\u4f46\u4e5f\u662f\u6709\u4e00\u4e9b\u4e0d\u540c\u7684\u5730\u65b9\uff0c\u8fd9\u91cc\u603b\u7ed3\u4e00\u4e0b\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<h5 class=\"wp-block-heading\">\u4ecejson\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u5b57\u6bb5<\/h5>\n\n\n\n<p>PostgreSQL \u7684\u5b98\u65b9\u6587\u6863\u771f\u7684\u5f88\u597d\uff0c\u5168\u9762\u4e14\u6709\u6837\u4f8b\uff0c\u6bd4\u8f83\u5bb9\u6613\u7406\u89e3\u548c\u4f7f\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6211\u4e60\u60ef\u5e38\u7528\u7684\u4e00\u822c\u662f -&gt;&gt; \u8fd9\u79cd\u53d6\u51fa\u7ed3\u679c\u4e3atext\u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\uff0c\u65b9\u4fbf\u6bd4\u8f83\nselect\n '&#91;1,2,3]'::json-&gt;2 as get_json_arr_by_index -- 3\n,'&#91;1,2,3]'::json-&gt;&gt;2 as get_json_arr_by_index_as_text -- 3\n,'{\"a\":1,\"b\":2}'::json-&gt;'b' as get_json_str_by_key -- 2\n,'{\"a\":1,\"b\":2}'::json-&gt;&gt;'b' as get_json_str_by_key_as_text -- 2\n,'{\"a\":&#91;1,2,3],\"b\":&#91;4,5,6]}'::json#&gt;'{a,2}' as get_json_str_by_path -- 3\n,'{\"a\":&#91;1,2,3],\"b\":&#91;4,5,6]}'::json#&gt;&gt;'{a,2}' as get_json_str_by_path_as_text -- 3\n,json_array_length('&#91;1,2,3,{\"f1\":1,\"f2\":&#91;5,6]},4]')    -- 5\n;\n\nselect\n*\nfrom\njson_each('{\"a\":\"foo\", \"b\":\"bar\"}')\n;\n\/*\n key | value\n-----+-------\n a   | \"foo\"\n b   | \"bar\"\n*\/\n\n-- \u4e0b\u9762\u8fd9\u4e00\u90e8\u5206\u548c\u4e0a\u9762\u7684\u663e\u793a\u8f93\u51fa\u662f\u76f8\u540c\u7684\uff0c\u4f46\u662f\u4e0b\u9762\u8fd9\u4e2a\u7684\u8fd4\u56de\u7684value\u7684\u7c7b\u578b\u662ftext\u7684\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u5b57\u7b26\u4e32\u6bd4\u8f83\uff0c\u4e0a\u9762\u90a3\u4e2a\u662fjson\u7c7b\u578b\u7684\nselect\n*\nfrom\njson_each_text('{\"a\":\"foo\", \"b\":\"bar\"}')\n;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">timestamp\u7c7b\u578b\u7684\u6bd4\u8f83<\/h5>\n\n\n\n<p>PostgreSQL \u6bd4\u8f83\u667a\u80fd\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u76f4\u63a5\u4f5c\u4e3a\u6761\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u624b\u52a8\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5728\u65e5\u671f\u65f6\u95f4\u5b57\u7b26\u4e32\u4e4b\u524d\u52a0\u4e0a timestamp \u5173\u952e\u5b57\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\nselect *\nfrom the_table\nwhere\nthe_timestamp_column &gt;= timestamp '2015-07-15 00:00:00'\nand the_timestamp_column &lt; timestamp '2015-07-16 00:00:00'\n;\n-- \u4e5f\u53ef\u4ee5\u76f4\u63a5\u6bd4\u8f83\nselect *\nfrom the_table\nwhere\nthe_timestamp_column &gt;= '2015-07-15'\nand the_timestamp_column &lt; '2015-07-16'\n;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u5b57\u7b26\u4e32\u5207\u5206\u548c\u5b50\u4e32\u63d0\u53d6\u7b49\u64cd\u4f5c<\/h5>\n\n\n\n<p>\u7b80\u5355\u6d4b\u8bd5\u4e86\u4e00\u4e0b\u53d1\u73b0 PostgreSQL \u652f\u6301\u7684\u529f\u80fd\u771f\u7684\u5f88\u591a\u800c\u4e14\u5f88\u65b9\u4fbf\uff0c\u66f4\u591a\u7684\u529f\u80fd\u70b9\u540e\u9762\u6839\u636e\u5b9e\u9645\u9700\u6c42\u6162\u6162\u63a2\u7d22\u5427<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select\n-- \u5b57\u7b26\u4e32\u5207\u5206\nsplit_part('abc~@~def~@~ghi', '~@~', 2) -- def\n,regexp_split_to_array('abc~@~def~@~ghi', '~@~') -- &#91;\"abc\",\"def\",\"ghi\"]\n,regexp_split_to_array('hello world', '\\s+') -- &#91;\"hello\",\"world\"]\n\n-- \u5b57\u7b26\u4e32\u67e5\u627e\u5b9a\u4f4d\n,strpos('high', 'ig') -- 2\n,position('ig' in 'high') -- 2 #\u548c\u4e0a\u9762\u7684\u5199\u6cd5\u7b49\u4ef7\n,strpos('high', 'x') -- 0 #\u627e\u4e0d\u5230\u7684\u65f6\u5019\u8fd4\u56de0\uff0c\u56e0\u4e3a\u627e\u5230\u7684\u65f6\u5019\u662f\u4ece1\u5f00\u59cb\u8ba1\u7b97\n\n-- \u5b57\u7b26\u4e32\u5b50\u4e32\n,left('abcde', 2) -- ab\n,right('abcde', 2) -- de\n,substr('alphabet', 3, 2) -- ph #\u8bf4\u660eindex\u4ece1\u5f00\u59cb\uff0c\u800c\u4e0d\u662f0\n,substring('alphabet' from 3 for 2) -- ph #\u548c\u4e0a\u9762\u7684\u5199\u6cd5\u7b49\u4ef7\n\n-- \u5b57\u7b26\u4e32\u6620\u5c04\u8f6c\u6362\/\u66ff\u6362\n,translate('12345', '135', 'abc') -- a2b4c\n\n,replace('abcdefabcdef', 'cd', 'XX') -- abXXefabXXef\n,regexp_replace('Thomas', '.&#91;mN]a.', 'M') -- ThM\n\n,ltrim('zzzytest', 'xyz') -- test\n,rtrim('testxxzx', 'xyz') -- test\n,lpad('hi', 5, 'xy') -- xyxhi\n,rpad('hi', 5, 'xy') -- hixyx\n\n-- \u5b57\u7b26\u4e32\u7ffb\u8f6c\n,reverse('abcde') -- edcba\n\n-- \u8ba1\u7b97\u5b57\u7b26\u4e32\u7684md5\u54c8\u5e0c\u503c\n,md5('abc')\n\n-- \u7f16\u7801\/\u89e3\u7801\n,encode('123\\000\\001', 'base64')\n,decode('MTIzAAE=', 'base64')\n\n-- \u5b57\u7b26\u4e32\u957f\u5ea6\u8ba1\u7b97\n,length('jose')\n,length('jose', 'UTF8')\n,char_length('jose')\n\n-- \u5b57\u7b26\u4e32\u8fde\u63a5\n,concat('abcde', 2, NULL, 22) -- abcde222\n,concat_ws(',', 'abcde', 2, NULL, 22) -- abcde,2,22\n\n,ascii('x') -- 120\n,chr(120) -- x\n\n,lower('TOM') -- tom\n,upper('tom') -- TOM\n;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u6a21\u5f0f\u5339\u914d\/\u6b63\u5219\u5339\u914d<\/h5>\n\n\n\n<p>\u5148\u4e86\u89e3\u4e00\u4e0b\u7b80\u5355\u548c\u5e38\u7528\u7684\u5339\u914d\u65b9\u6cd5\uff0c\u540e\u9762\u6839\u636e\u5b9e\u9645\u9700\u6c42\u6162\u6162\u6574\u7406\u603b\u7ed3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select\n'abc' LIKE 'abc' -- true\n,'abc' LIKE 'a%'  -- true\n,'abc' LIKE '_b_' -- true\n,'abc' LIKE 'c'   -- false\n\n,'abc' SIMILAR TO 'abc'     -- true\n,'abc' SIMILAR TO 'a'       -- false\n,'abc' SIMILAR TO '%(b|d)%' -- true\n,'abc' SIMILAR TO '(b|c)%'  -- false\n\n,'abc' ~ 'abc'    -- true\n,'abc' ~ '^a'     -- true\n,'abc' ~ '(b|d)'  -- true\n,'abc' ~ '^(b|c)' -- false\n;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u7c7b\u578b\u8f6c\u6362<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6574\u6570integer\u7c7b\u578b\nSELECT\nNULLIF(your_value, '')::int\n,CAST(coalesce(your_value, '0') AS integer) as new_field\n;\n\n-- If you need to treat empty columns as NULLs, try this:\nSELECT CAST(nullif(&lt;column&gt;, '') AS integer);\n\n-- On the other hand, if you do have NULL values that you need to avoid, try:\nSELECT CAST(coalesce(&lt;column&gt;, '0') AS integer);<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u65e5\u671f\u65f6\u95f4\u7684\u5904\u7406<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\nCURRENT_DATE\n,date(now())\n,now()\n-- ,CURRENT_TIME\n,CURRENT_TIMESTAMP\n-- ,CURRENT_TIME(2)\n,CURRENT_TIMESTAMP(2)\n;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>9.15. JSON Functions and Operators<br><a href=\"https:\/\/www.postgresql.org\/docs\/9.3\/functions-json.html\">https:\/\/www.postgresql.org\/docs\/9.3\/functions-json.html<\/a><\/p>\n\n\n\n<p>Postgres where clause compare timestamp<br><a href=\"https:\/\/stackoverflow.com\/questions\/31433747\/postgres-where-clause-compare-timestamp\">https:\/\/stackoverflow.com\/questions\/31433747\/postgres-where-clause-compare-timestamp<\/a><\/p>\n\n\n\n<p>9.4. String Functions and Operators<br><a href=\"https:\/\/www.postgresql.org\/docs\/9.3\/functions-string.html\">https:\/\/www.postgresql.org\/docs\/9.3\/functions-string.html<\/a><\/p>\n\n\n\n<p>9.7. Pattern Matching<br><a href=\"https:\/\/www.postgresql.org\/docs\/9.3\/functions-matching.html\">https:\/\/www.postgresql.org\/docs\/9.3\/functions-matching.html<\/a><\/p>\n\n\n\n<p>How to split string with different characters in postgresql<br><a href=\"https:\/\/stackoverflow.com\/questions\/24236167\/how-to-split-string-with-different-characters-in-postgresql\">https:\/\/stackoverflow.com\/questions\/24236167\/how-to-split-string-with-different-characters-in-postgresql<\/a><\/p>\n\n\n\n<p>Typecast string to integer<br><a href=\"https:\/\/stackoverflow.com\/questions\/10518258\/typecast-string-to-integer\">https:\/\/stackoverflow.com\/questions\/10518258\/typecast-string-to-integer<\/a><\/p>\n\n\n\n<p>9.9. Date\/Time Functions and Operators<br><a href=\"https:\/\/www.postgresql.org\/docs\/9.3\/functions-datetime.html\">https:\/\/www.postgresql.org\/docs\/9.3\/functions-datetime.html<\/a><\/p>\n\n\n\n<p>\u6807\u7b7e\uff1a PostgreSQL<br><a href=\"https:\/\/ixyzero.com\/blog\/archives\/tag\/postgresql\">https:\/\/ixyzero.com\/blog\/archives\/tag\/postgresql<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5f88\u65e9\u5c31\u542c\u8bf4\u8fc7 PostgreSQL \u8fd9\u4e2a\u6570\u636e\u5e93\uff0c\u8fd1\u671f\u7ffb\u9605blog\u7684\u65f6\u5019\u53d1\u73b014\/15\u5e74 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,23,7],"tags":[1413,1927,1925,404,1926,1262],"class_list":["post-5421","post","type-post","status-publish","format-standard","hentry","category-database","category-knowledgebase-2","category-programing","tag-cast","tag-coalesce","tag-json_each","tag-postgresql","tag-split_part","tag-sql"],"views":975,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=5421"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5421\/revisions"}],"predecessor-version":[{"id":5422,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5421\/revisions\/5422"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}