{"id":207666,"date":"2025-07-08T13:10:58","date_gmt":"2025-07-08T05:10:58","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207666\/"},"modified":"2025-07-08T13:10:58","modified_gmt":"2025-07-08T05:10:58","slug":"postgres-%e4%b8%ad%e7%9a%84-go-%e5%92%8c-in-%e5%ad%90%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207666\/","title":{"rendered":"Postgres \u4e2d\u7684 Go \u548c IN \u5b50\u53e5"},"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>Postgres \u4e2d\u7684 Go \u548c IN \u5b50\u53e5<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 13:54: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>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aPostgres \u4e2d\u7684 Go \u548c IN \u5b50\u53e5\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u4f7f\u7528 pq \u9a71\u52a8\u7a0b\u5e8f\u5bf9 go \u4e2d\u7684 postgresql \u6570\u636e\u5e93\u6267\u884c\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>select count(id)\nfrom tags\nwhere id in (1, 2, 3)<\/pre>\n<p>\u5176\u4e2d <code>1, 2, 3<\/code> \u5728\u5207\u7247 <code>tags := []string{\"1\", \"2\", \"3\"}<\/code> \u5904\u4f20\u9012\u3002<\/p>\n<p>\u6211\u5c1d\u8bd5\u8fc7\u5f88\u591a\u4e0d\u540c\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>s := \"(\" + strings.join(tags, \",\") + \")\"\nif err := db.queryrow(`\n    select count(id)\n    from tags\n    where id in $1`, s,\n).scan(&amp;num); err != nil {\n    log.println(err)\n}<\/pre>\n<p>\u8fd9\u4f1a\u5bfc\u81f4 <code>pq: \u5728\u201c$1\u201d<\/code> \u5904\u6216\u9644\u8fd1\u51fa\u73b0\u8bed\u6cd5\u9519\u8bef\u3002\u6211\u4e5f\u5c1d\u8bd5\u8fc7<\/p>\n<pre>if err := Db.QueryRow(`\n    SELECT COUNT(id)\n    FROM tags\n    WHERE id IN ($1)`, strings.Join(stringTagIds, \",\"),\n).Scan(&amp;num); err != nil {\n    log.Println(err)\n}<\/pre>\n<p>\u4e5f\u56e0 <code>pq \u5931\u8d25\uff1a\u6574\u6570\u8f93\u5165\u8bed\u6cd5\u65e0\u6548\uff1a\u201c1,2,3\u201d<\/code><\/p>\n<p>\u6211\u8fd8\u5c1d\u8bd5\u76f4\u63a5\u4f20\u9012\u6574\u6570\/\u5b57\u7b26\u4e32\u5207\u7247\uff0c\u5e76\u5f97\u5230 <code>sql: conversion exec argument #0's type: unsupported type []string, a slice<\/code>\u3002<\/p>\n<p>\u90a3\u4e48\u6211\u5982\u4f55\u5728 go \u4e2d\u6267\u884c\u8fd9\u4e2a\u67e5\u8be2\u5462\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<h3>\u9884\u6784\u5efa sql \u67e5\u8be2\uff08\u9632\u6b62 sql \u6ce8\u5165\uff09<\/h3>\n<p>\u5982\u679c\u60a8\u8981\u4e3a\u6bcf\u4e2a\u503c\u751f\u6210\u5e26\u6709\u53c2\u6570\u5360\u4f4d\u7b26\u7684 sql \u5b57\u7b26\u4e32\uff0c\u5219\u7acb\u5373\u751f\u6210\u6700\u7ec8 sql \u4f1a\u66f4\u5bb9\u6613\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u503c\u662f <code>string<\/code>s\uff0c\u56e0\u6b64\u5b58\u5728 sql \u6ce8\u5165\u653b\u51fb\u7684\u5730\u65b9\uff0c\u56e0\u6b64\u6211\u4eec\u9996\u5148\u6d4b\u8bd5\u6240\u6709 <code>string<\/code> \u503c\u662f\u5426\u786e\u5b9e\u662f\u6570\u5b57\uff0c\u53ea\u6709\u8fd9\u6837\u6211\u4eec\u624d\u7ee7\u7eed\uff1a<\/p>\n<pre>tags := []string{\"1\", \"2\", \"3\"}\nbuf := bytes.newbufferstring(\"select count(id) from tags where id in(\")\nfor i, v := range tags {\n    if i &gt; 0 {\n        buf.writestring(\",\")\n    }\n    if _, err := strconv.atoi(v); err != nil {\n        panic(\"not number!\")\n    }\n    buf.writestring(v)\n}\nbuf.writestring(\")\")<\/pre>\n<p>\u6267\u884c\uff1a<\/p>\n<pre>num := 0\nif err := db.queryrow(buf.string()).scan(&amp;num); err != nil {\n    log.println(err)\n}<\/pre>\n<h3>\u4f7f\u7528 <code>any<\/code><\/h3>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\uff0c\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>expression operator any (array expression)<\/pre>\n<p>\u4f7f\u7528\u5b83\uff0c\u6211\u4eec\u7684\u67e5\u8be2\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>select count(id) from tags where id = any('{1,2,3}'::int[])<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u4ee5\u5c06\u6570\u7ec4\u7684\u6587\u672c\u5f62\u5f0f\u58f0\u660e\u4e3a\u53c2\u6570\uff1a<\/p>\n<pre>select count(id) from tags where id = any($1::int[])<\/pre>\n<p>\u53ef\u4ee5\u7b80\u5355\u5730\u50cf\u8fd9\u6837\u6784\u5efa\uff1a<\/p>\n<pre>tags := []string{\"1\", \"2\", \"3\"}\nparam := \"{\" + strings.join(tags, \",\") + \"}\"<\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e0d\u9700\u8981\u68c0\u67e5\uff0c\u56e0\u4e3a\u6570\u7ec4\u8868\u8fbe\u5f0f\u4e0d\u5141\u8bb8 sql \u6ce8\u5165\uff08\u800c\u662f\u4f1a\u5bfc\u81f4\u67e5\u8be2\u6267\u884c\u9519\u8bef\uff09\u3002<\/p>\n<p>\u5b8c\u6574\u4ee3\u7801\uff1a<\/p>\n<pre>tags := []string{\"1\", \"2\", \"3\"}\n\nq := \"select count(id) from tags where id = any($1::int[])\"\nparam := \"{\" + strings.join(tags, \",\") + \"}\"\n\nnum := 0\nif err := db.queryrow(q, param).scan(&amp;num); err != nil {\n    log.println(err)\n}<\/pre>\n<p>\u8fd9\u5e76\u4e0d\u662f\u771f\u6b63\u7684 golang \u95ee\u9898\uff0c\u60a8\u6b63\u5728\u4f7f\u7528\u5b57\u7b26\u4e32\u4e0e sql \u8bf7\u6c42\u4e2d\u7684\u6574\u6570 (id) \u8fdb\u884c\u6bd4\u8f83\u3002\u8fd9\u610f\u5473\u7740\uff0csql \u63a5\u6536\uff1a<\/p>\n<pre>select count(id)\nfrom tags\nwhere id in (\"1, 2, 3\")<\/pre>\n<p>\u800c\u4e0d\u662f\u4f60\u60f3\u8981\u7ed9\u5b83\u7684\u4e1c\u897f\u3002\u60a8\u53ea\u9700\u5c06\u6807\u7b7e\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u5c06\u5176\u4f20\u9012\u7ed9\u67e5\u8be2\u5373\u53ef\u3002<\/p>\n<p>\u7f16\u8f91\uff1a \u7531\u4e8e\u60a8\u5c1d\u8bd5\u5c06\u591a\u4e2a\u503c\u4f20\u9012\u7ed9\u67e5\u8be2\uff0c\u90a3\u4e48\u60a8\u5e94\u8be5\u544a\u8bc9\u5b83\uff1a<\/p>\n<pre>params := make([]string, 0, len(tags))\nfor i := range tags {\n    params = append(params, fmt.sprintf(\"$%d\", i+1))\n}\nquery := fmt.sprintf(\"select count(id) from tags where id in (%s)\", strings.join(params, \", \"))<\/pre>\n<p>\u8fd9\u5c06\u4ee5\u201c($1, $2, $3&#8230;\u201d\u7ed3\u675f\u67e5\u8be2\uff0c\u7136\u540e\u5c06\u6807\u7b7e\u8f6c\u6362\u4e3a int\uff1a<\/p>\n<pre>values := make([]int, 0, len(tags))\nfor _, s := range tags {\n    val, err := strconv.atoi(s)\n    if err != nil {\n        \/\/ do whatever is required with the error\n        fmt.println(\"err : \", err)\n    } else {\n        values = append(values, val)\n    }\n}<\/pre>\n<p>\u6700\u540e\uff0c\u60a8\u53ef\u4ee5\u5728\u67e5\u8be2\u4e2d\u4f7f\u7528\u5b83\uff1a<\/p>\n<pre>Db.QueryRow(query, values...)<\/pre>\n<p>\u8fd9\u5e94\u8be5\u53ef\u4ee5\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPostgres \u4e2d\u7684 Go \u548c IN \u5b50\u53e5\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\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-207666","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207666","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=207666"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207666\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}