{"id":233396,"date":"2022-01-12T10:13:55","date_gmt":"2022-01-12T02:13:55","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=233396"},"modified":"2021-12-30T13:19:33","modified_gmt":"2021-12-30T05:19:33","slug":"postgresql-linux-go","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/postgresql-linux-go.html","title":{"rendered":"\u89e3\u6790postgresql \u5220\u9664\u91cd\u590d\u6570\u636e\u6848\u4f8b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86postgresql \u5220\u9664\u91cd\u590d\u6570\u636e\u6848\u4f8b\u8be6\u89e3,\u672c\u7bc7\u6587\u7ae0\u901a\u8fc7\u7b80\u8981\u7684\u6848\u4f8b,\u8bb2\u89e3\u4e86\u8be5\u9879\u6280\u672f\u7684\u4e86\u89e3\u4e0e\u4f7f\u7528,\u4ee5\u4e0b\u5c31\u662f\u8be6\u7ec6\u5185\u5bb9,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1.\u5efa\u8868<\/strong><\/span><\/div>\n
\/*\r\n Navicat Premium Data Transfer\r\n \r\n Source Server         : localhost\r\n Source Server Type    : PostgreSQL\r\n Source Server Version : 110012\r\n Source Host           : localhost:5432\r\n Source Catalog        : postgres\r\n Source Schema         : public\r\n \r\n Target Server Type    : PostgreSQL\r\n Target Server Version : 110012\r\n File Encoding         : 65001\r\n \r\n Date: 30\/07\/2021 10:10:04\r\n*\/\r\n \r\n \r\n-- ----------------------------\r\n-- Table structure for test\r\n-- ----------------------------\r\nDROP TABLE IF EXISTS \"public\".\"test\";\r\nCREATE TABLE \"public\".\"test\" (\r\n  \"id\" int4 NOT NULL DEFAULT NULL,\r\n  \"name\" varchar(255) COLLATE \"pg_catalog\".\"default\" DEFAULT NULL,\r\n  \"age\" int4 DEFAULT NULL\r\n)\r\n;\r\n \r\n-- ----------------------------\r\n-- Records of test\r\n-- ----------------------------\r\nINSERT INTO \"public\".\"test\" VALUES (1, 'da', 1);\r\nINSERT INTO \"public\".\"test\" VALUES (2, 'da', 12);\r\nINSERT INTO \"public\".\"test\" VALUES (3, 'dd', 80);\r\nINSERT INTO \"public\".\"test\" VALUES (4, 'dd', 80);\r\nINSERT INTO \"public\".\"test\" VALUES (5, 'd1', 13);\r\n \r\n-- ----------------------------\r\n-- Primary Key structure for table test\r\n-- ----------------------------\r\nALTER TABLE \"public\".\"test\" ADD CONSTRAINT \"test_pkey\" PRIMARY KEY (\"id\");<\/pre>\n
2.\u6839\u636e\u540d\u79f0\u83b7\u53d6\u91cd\u590d<\/strong><\/span><\/div>\n

\u5148\u770b\u770b\u54ea\u4e9b\u6570\u636e\u91cd\u590d\u4e86<\/p>\n

select name ,count(1)  from test group by name  having count(1)>1<\/pre>\n

\u8f93\u51fa.<\/p>\n

name        count\r\n\r\nda              2\r\n\r\ndd              2\r\n<\/pre>\n
3.\u5220\u9664\u6240\u6709\u91cd\u590d\u6570\u636e<\/strong><\/span><\/div>\n

\u6ce8\u610f\u628a\u8981\u66f4\u65b0\u7684\u51e0\u5217\u6570\u636e\u67e5\u8be2\u51fa\u6765\u505a\u4e3a\u4e00\u4e2a\u7b2c\u4e09\u65b9\u8868\uff0c\u7136\u540e\u7b5b\u9009\u66f4\u65b0\u3002<\/p>\n

delete from test where name in (select t.name from (select name ,count(1)  from test group by name  having count(1)>1) t)<\/pre>\n
4.\u4fdd\u7559\u4e00\u884c\u6570\u636e<\/strong><\/span><\/div>\n

\u8fd9\u91cc\u5c55\u793a\u6211\u4eec\u9700\u8981\u4fdd\u7559\u7684\u6570\u636e\uff1a\u91cd\u590d\u6570\u636e\uff0c\u4fdd\u7559ID\u6700\u5927\u90a3\u4e00\u6761<\/p>\n

\r\nSELECT\r\n 1. \r\nFROM\r\n test \r\nWHERE\r\n id NOT IN (\r\n ( SELECT min( id ) AS id FROM test GROUP BY name ) \r\n )<\/pre>\n
5.\u5220\u9664\u6570\u636e<\/strong><\/span><\/div>\n
\r\nDELETE\r\nFROM\r\n test \r\nWHERE\r\n id NOT IN (\r\n SELECT\r\n  t.id \r\n FROM\r\n ( SELECT max( id ) AS id FROM test GROUP BY name ) t \r\n )<\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8epostgresql \u5220\u9664\u91cd\u590d\u6570\u636e\u6848\u4f8b\u8be6\u89e3\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\/* Navicat Premium Data Transfer Source Server : localh […]<\/p>\n","protected":false},"author":1482,"featured_media":118928,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-233396","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=233396"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233396\/revisions"}],"predecessor-version":[{"id":233399,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233396\/revisions\/233399"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/118928"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=233396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=233396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=233396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}