{"id":207296,"date":"2025-07-08T10:49:20","date_gmt":"2025-07-08T02:49:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207296\/"},"modified":"2025-07-08T10:49:20","modified_gmt":"2025-07-08T02:49:20","slug":"go-pg-unionall-%e9%99%90%e5%88%b6%e6%95%b4%e4%b8%aa%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207296\/","title":{"rendered":"go-pg UnionAll &#8211; \u9650\u5236\u6574\u4e2a\u8868\u8fbe\u5f0f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>go-pg UnionAll &#8211; \u9650\u5236\u6574\u4e2a\u8868\u8fbe\u5f0f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 15:33:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>Golang\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300ago-pg UnionAll &#8211; \u9650\u5236\u6574\u4e2a\u8868\u8fbe\u5f0f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u5728 go-pg \u5e93\u4e2d\u4e3a golang \u4f7f\u7528 <code>.unionall<\/code> \u65b9\u6cd5\u3002<\/p>\n<pre>    var model []customer\n    q0 := db.model(&amp;model).where(\"name = ?\", name0).limit(4)\n    q1 := db.model(&amp;model).where(\"name = ?\", name1).limit(3)\n    var result []customer\n    if err := q0.unionall(q1).limit(1).select(&amp;result); !as.noerror(err) {\n        return\n    }\n<\/pre>\n<p>\u6b64\u4ee3\u7801\u751f\u6210\u67e5\u8be2\uff1a<\/p>\n<pre>(select \"customer\".\"id\", \"customer\".\"name\" from customers as \"customer\" where (name = 'customer 1deificatory zonoid reprepare alacrify serenissime') \nlimit 1) \nunion all \n(select \"customer\".\"id\", \"customer\".\"name\" from customers as \"customer\" where (name = 'customer 2fordless ferroboron radiability dandizette smutch'\nlimit 3)\n)\n<\/pre>\n<p>\u4f46\u6211\u5e0c\u671b\u5b83\u662f\uff1a<\/p>\n<pre>(SELECT \"customer\".\"id\", \"customer\".\"name\" FROM customers AS \"customer\" WHERE (name = 'customer 1deificatory zonoid reprepare alacrify serenissime') \nLIMIT 4) \nUNION ALL \n(SELECT \"customer\".\"id\", \"customer\".\"name\" FROM customers AS \"customer\" WHERE (name = 'customer 2fordless ferroboron radiability dandizette smutch')\nLIMIT 3)\n\nLIMIT 1\n<\/pre>\n<p>\u90a3\u4e48\u5982\u4f55\u4f7f\u7528 go-pg \u6765\u83b7\u53d6\u6211\u671f\u671b\u7684\u539f\u59cb sql \u67e5\u8be2\u5462\uff1f \u95ee\u9898\u662f\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\u6211\u65e0\u6cd5\u5c06 <code>limit 1<\/code> \u8868\u8fbe\u5f0f\u5e94\u7528\u4e8e\u6574\u4e2a\u67e5\u8be2\u3002 \u53e6\u5916\uff0c\u6211\u7684 <code>limit 4<\/code> \u672a\u6b63\u786e\u5e94\u7528\u4e8e\u7b2c\u4e00\u4e2a <code>union all<\/code> \u6210\u5458\u3002 \u6211\u7684\u5168\u90e8\u4ee3\u7801\u90fd\u5728\u8fd9\u91cc\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u76f8\u4fe1\u8fd9\u4e2a\u67e5\u8be2\u5bf9\u4f60\u6709\u7528\u3002<\/p>\n<pre>if err := db.Model().With(\"union_q\", q0.UnionAll(q1)).Table(\"union_q\").Limit(1).Select(&amp;result); !as.NoError(err) {\n    return\n}<\/pre>\n<p>\u8be5\u67e5\u8be2\u5e76\u4e0d\u5b8c\u5168\u662f\u60a8\u60f3\u8981\u7684\uff0c\u636e\u6211\u6240\u77e5\uff0c\u5f53\u524d\u7248\u672c\u7684 go-pg \u65e0\u6cd5\u751f\u6210\u8be5\u67e5\u8be2\u3002\u4f46\u8fd9\u4e2a\u67e5\u8be2\u5b8c\u5168\u7b26\u5408\u60a8\u7684\u8981\u6c42\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300ago-pg UnionAll &#8211; \u9650\u5236\u6574\u4e2a\u8868\u8fbe\u5f0f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207296","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207296","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=207296"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207296\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}