{"id":207049,"date":"2025-07-08T09:00:41","date_gmt":"2025-07-08T01:00:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207049\/"},"modified":"2025-07-08T09:00:41","modified_gmt":"2025-07-08T01:00:41","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-gorm-%e8%8e%b7%e5%be%97%e6%98%8e%e6%98%be%e7%9a%84%e7%bb%93%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207049\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 GORM \u83b7\u5f97\u660e\u663e\u7684\u7ed3\u679c"},"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>\u5982\u4f55\u4f7f\u7528 GORM \u83b7\u5f97\u660e\u663e\u7684\u7ed3\u679c<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 18:03:20<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 GORM \u83b7\u5f97\u660e\u663e\u7684\u7ed3\u679c\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728 go \u4e2d\uff0c\u6211\u7f16\u5199\u4e86\u4e00\u4e2a\u67e5\u8be2\u6765\u63d0\u4f9b\u6240\u6709\u6570\u636e\uff0c\u4f46\u6211\u53ea\u60f3\u83b7\u53d6 products.id \u548c client.id \u4e0d\u540c\u7684\u6570\u636e\u3002 \u6211\u53ef\u4ee5\u5199\u4ec0\u4e48\u660e\u55bb\u67e5\u8be2\uff1f<\/p>\n<pre>res := find.Model(&amp;domain.Clients{}).\n    Select (\"products.id product_id, products.name product_name,\"+\n        \" clients.id id, clients.name name, clients.logo, clients.address, \"+\n        \"clients.business_id, clients.num_of_employee, clients.email, clients.sns_link, clients.phone\").\n    Joins(\"LEFT JOIN company_interests ON company_interests.client_id = clients.id\").\n    Joins(\"LEFT JOIN products ON products.id = company_interests.product_id\").\n    Where(\"products.id = ? \", productId).Find(&amp;resp)<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5728go\u4e2d\uff0c\u5f53\u6211\u5199\u201cselect distinct\u201d\u7136\u540e\u4f11\u606f\u67e5\u8be2\u65f6\uff0c\u5b83\u5728go\u4e2d\u65e0\u6548\u3002\u6240\u4ee5\uff0c\u6211\u60f3\u5230\u4e86\u4f7f\u7528\u201cgroup by\u201d\u7f16\u5199\u67e5\u8be2\u3002\u5728go\u4e2d\uff0c\u201cgroup by\u201d\u8bed\u6cd5\u53ef\u4ee5\u901a\u8fc7\u201cgroup\u201d\u8bed\u6cd5\u6765\u4f7f\u7528\u3002\u6240\u4ee5\uff0c\u6700\u540e\u4e0b\u9762\u7684\u67e5\u8be2\u5bf9\u6211\u6765\u8bf4\u6548\u679c\u5f88\u597d\u3002<\/p>\n<pre>res := find.Model(&amp;domain.Clients{}).\n    Select (\"products.id product_id, products.name product_name,\"+\n        \" clients.id id, clients.name name, clients.logo, clients.address, \"+\n        \"clients.business_id, clients.num_of_employee, clients.email, clients.sns_link, clients.phone\").\n    Joins(\"LEFT JOIN company_interests ON company_interests.client_id = clients.id\").\n    Joins(\"LEFT JOIN products ON products.id = company_interests.product_id\").\n    Where(\"products.id = ? \", productId).\n    Group(\"company_interests.client_id, company_interests.product_id\" ).Find(&amp;resp)<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528 GORM \u83b7\u5f97\u660e\u663e\u7684\u7ed3\u679c\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/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-207049","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207049","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=207049"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207049\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}