{"id":21429,"date":"2024-01-02T09:14:26","date_gmt":"2024-01-02T09:14:26","guid":{"rendered":"https:\/\/server.hk\/blog\/21429\/"},"modified":"2024-11-14T10:34:34","modified_gmt":"2024-11-14T02:34:34","slug":"how-to-fix-postgresql-error-code-42p09-ambiguous_alias","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/21429\/","title":{"rendered":"\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a42P09 &#8211; ambiguous_alias"},"content":{"rendered":"<h1 id=\"how-to-fix-postgresql-error-code-42p09-ambiguous_alias-dnfSdyLVEk\">\u5982\u4f55\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a42P09 &#8211; ambiguous_alias<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u3002\u7136\u800c\uff0c\u50cf\u5176\u4ed6\u8edf\u4ef6\u4e00\u6a23\uff0c\u5b83\u4e5f\u53ef\u80fd\u9047\u5230\u6703\u59a8\u7919\u5176\u9806\u5229\u904b\u4f5c\u7684\u932f\u8aa4\u3002\u5176\u4e2d\u4e00\u500b\u932f\u8aa4\u662f PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a42P09 &#8211; ambiguous_alias\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e\u6b64\u932f\u8aa4\u7684\u542b\u7fa9\u4ee5\u53ca\u5982\u4f55\u4fee\u5fa9\u5b83\u3002<\/p>\n<h2 id=\"understanding-the-error-dnfSdyLVEk\">\u7406\u89e3\u932f\u8aa4<\/h2>\n<p>PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a42P09 &#8211; ambiguous_alias \u767c\u751f\u5728\u67e5\u8a62\u4e2d\u7684\u5217\u6216\u8868\u5225\u540d\u5b58\u5728\u6b67\u7fa9\u6642\u3002\u9019\u610f\u5473\u8457\u6578\u64da\u5eab\u5f15\u64ce\u7121\u6cd5\u78ba\u5b9a\u6b63\u5728\u5f15\u7528\u54ea\u500b\u8868\u6216\u5217\uff0c\u56e0\u70ba\u5b58\u5728\u591a\u500b\u540c\u540d\u7684\u5225\u540d\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u8003\u616e\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT t1.id, t2.id\r\nFROM table1 AS t1\r\nJOIN table2 AS t2 ON t1.id = t2.id;\r\n<\/code><\/pre>\n<p>\u5728\u6b64\u67e5\u8a62\u4e2d\uff0c<code>t1<\/code> \u548c <code>t2<\/code> \u90fd\u6709\u76f8\u540c\u7684\u5217\u540d <code>id<\/code>\u3002\u57f7\u884c\u6b64\u67e5\u8a62\u6642\uff0cPostgreSQL \u5c07\u62cb\u51fa ambiguous_alias \u932f\u8aa4\uff0c\u56e0\u70ba\u5b83\u7121\u6cd5\u78ba\u5b9a\u5728 <code>SELECT<\/code> \u8a9e\u53e5\u4e2d\u5f15\u7528\u7684\u662f\u54ea\u4e00\u500b <code>id<\/code> \u5217\u3002<\/p>\n<h2 id=\"fixing-the-error-dnfSdyLVEk\">\u4fee\u5fa9\u932f\u8aa4<\/h2>\n<p>\u8981\u4fee\u5fa9 PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a42P09 &#8211; ambiguous_alias\uff0c\u60a8\u9700\u8981\u70ba\u885d\u7a81\u7684\u5217\u6216\u8868\u63d0\u4f9b\u660e\u78ba\u7684\u5225\u540d\u3002\u4ee5\u4e0b\u662f\u5e7e\u7a2e\u53ef\u4ee5\u63a1\u53d6\u7684\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"1-specify-the-table-alias-dnfSdyLVEk\">1. \u6307\u5b9a\u8868\u5225\u540d<\/h3>\n<p>\u89e3\u6c7a\u6b64\u932f\u8aa4\u7684\u4e00\u7a2e\u65b9\u6cd5\u662f\u660e\u78ba\u6307\u5b9a\u8868\u5225\u540d\u4f86\u5f15\u7528\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT t1.id, t2.id\r\nFROM table1 AS t1\r\nJOIN table2 AS t2 ON t1.id = t2.id;\r\n<\/code><\/pre>\n<p>\u901a\u904e\u5728\u5217\u540d\u524d\u52a0\u4e0a\u9069\u7576\u7684\u8868\u5225\u540d\uff0c\u60a8\u6d88\u9664\u4e86\u6b67\u7fa9\uff0cPostgreSQL \u53ef\u4ee5\u6b63\u78ba\u8b58\u5225\u8a72\u5217\u3002<\/p>\n<h3 id=\"2-use-column-aliases-dnfSdyLVEk\">2. \u4f7f\u7528\u5217\u5225\u540d<\/h3>\n<p>\u53e6\u4e00\u7a2e\u65b9\u6cd5\u662f\u4f7f\u7528\u5217\u5225\u540d\u4f86\u5340\u5206\u885d\u7a81\u7684\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT t1.id AS t1_id, t2.id AS t2_id\r\nFROM table1 AS t1\r\nJOIN table2 AS t2 ON t1.id = t2.id;\r\n<\/code><\/pre>\n<p>\u901a\u904e\u70ba\u5217\u5206\u914d\u552f\u4e00\u7684\u5225\u540d\uff0c\u60a8\u6d88\u9664\u4e86\u6b67\u7fa9\uff0cPostgreSQL \u53ef\u4ee5\u6b63\u78ba\u89e3\u91cb\u67e5\u8a62\u3002<\/p>\n<h3 id=\"3-qualify-column-names-dnfSdyLVEk\">3. \u9644\u52a0\u5217\u540d\u9650\u5b9a<\/h3>\n<p>\u5982\u679c\u60a8\u4e0d\u60f3\u4f7f\u7528\u5225\u540d\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u8868\u540d\u4f86\u9650\u5b9a\u5217\u540d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT table1.id, table2.id\r\nFROM table1\r\nJOIN table2 ON table1.id = table2.id;\r\n<\/code><\/pre>\n<p>\u901a\u904e\u660e\u78ba\u6307\u5b9a\u8868\u540d\u548c\u5217\u540d\uff0c\u60a8\u6d88\u9664\u4e86\u4efb\u4f55\u6b67\u7fa9\uff0cPostgreSQL \u53ef\u4ee5\u7121\u932f\u8aa4\u5730\u89e3\u6790\u67e5\u8a62\u3002<\/p>\n<h2 id=\"conclusion-dnfSdyLVEk\">\u7d50\u8ad6<\/h2>\n<p>PostgreSQL \u932f\u8aa4\u4ee3\u78bc\uff1a42P09 &#8211; ambiguous_alias \u53ef\u4ee5\u901a\u904e\u5728\u67e5\u8a62\u4e2d\u63d0\u4f9b\u660e\u78ba\u7684\u5225\u540d\u6216\u9650\u5b9a\u5217\u540d\u4f86\u89e3\u6c7a\u3002\u901a\u904e\u9075\u5faa\u672c\u6587\u4e2d\u63d0\u5230\u7684\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u78ba\u4fdd PostgreSQL \u67e5\u8a62\u5728\u57f7\u884c\u6642\u4e0d\u6703\u9047\u5230\u6b64\u932f\u8aa4\u3002<\/p>\n<p>\u5982\u9700\u6709\u95dc VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\u7684\u66f4\u591a\u8cc7\u8a0a\uff0c\u8003\u616e\u63a2\u7d22 <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002\u6191\u85c9\u5176\u4e00\u6d41\u7684 VPS \u8a17\u7ba1\u670d\u52d9\uff0c\u60a8\u53ef\u4ee5\u9ad4\u9a57\u5230\u53ef\u9760\u4e14\u9ad8\u6548\u7684 PostgreSQL \u6578\u64da\u5eab\u8a17\u7ba1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4fee\u5fa9 PostgreSQL &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-21429","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=21429"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21429\/revisions"}],"predecessor-version":[{"id":24059,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/21429\/revisions\/24059"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=21429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=21429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=21429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}