pq: 函数 unnest(unknown) 不是唯一的
pq: function unnest(unknown) is not unique
以下代码运行良好。但是我想定义array['a', 'b', 'c', 'd', 'e'] 作为一个变量。
rows, err := db.Query("select colname from (SELECT date, unnest(array['a', 'b', 'c', 'd', 'e']) AS colname, unnest(array[a, b, c, d, e]) AS thing from test1 where date='123') as tester where thing=1;")
所以我尝试使用 github.com/lib/pq .
下面的代码
arr1 := []string{"a", "b", "c", "d", "e"}
rows, err := db.Query("select colname from (SELECT date, unnest() AS colname, unnest() AS thing from test1 where date='123') as tester where thing=1;", pq.Array(arr1))
但是出现 "pq: function unnest(unknown) is not unique" 这样的错误。
Table 结构和示例数据--
test=# \d+ test1
Table "public.test1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------+-----------+----------+--------------+-------------
a | character varying(10) | | extended | |
b | character varying(10) | | extended | |
c | character varying(10) | | extended | |
d | character varying(10) | | extended | |
e | character varying(10) | | extended | |
date | character varying(10) | | extended | |
test=# select * from test1 ;
a | b | c | d | e | date
---+---+---+---+---+------
3 | 1 | 3 | 2 | 3 | 124
3 | 3 | 2 | 2 | 1 | 125
1 | 2 | 2 | 1 | 3 | 126
1 | 2 | 3 | 2 | 3 | 127
1 | 1 | 2 | 2 | 3 | 123
(5 rows)
基本上我想要列名(a、b、c、d 或 e)在任何特定日期的值为“1”。
我猜想 pq.Array
给你一个字符串形式的 PostgreSQL 数组,所以你最终得到这样的东西:
unnest('{a,b,c,d,e}')
并且 PostgreSQL 不确定它应该如何解释该字符串,因此出现了关于 unnest(unknown)
的投诉。您应该能够添加显式类型转换来解决问题:
unnest(::text[]) -- PostgreSQL-specific casting syntax
unnest(cast( as text[])) -- Standard casting syntax
你最终会得到这样的结果:
rows, err := db.Query("select colname from (SELECT date, unnest(::text[]) AS colname, unnest() AS thing from test1 where date='123') as tester where thing=1;", pq.Array(arr1))
以下代码运行良好。但是我想定义array['a', 'b', 'c', 'd', 'e'] 作为一个变量。
rows, err := db.Query("select colname from (SELECT date, unnest(array['a', 'b', 'c', 'd', 'e']) AS colname, unnest(array[a, b, c, d, e]) AS thing from test1 where date='123') as tester where thing=1;")
所以我尝试使用 github.com/lib/pq .
下面的代码arr1 := []string{"a", "b", "c", "d", "e"}
rows, err := db.Query("select colname from (SELECT date, unnest() AS colname, unnest() AS thing from test1 where date='123') as tester where thing=1;", pq.Array(arr1))
但是出现 "pq: function unnest(unknown) is not unique" 这样的错误。 Table 结构和示例数据--
test=# \d+ test1
Table "public.test1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------+-----------+----------+--------------+-------------
a | character varying(10) | | extended | |
b | character varying(10) | | extended | |
c | character varying(10) | | extended | |
d | character varying(10) | | extended | |
e | character varying(10) | | extended | |
date | character varying(10) | | extended | |
test=# select * from test1 ;
a | b | c | d | e | date
---+---+---+---+---+------
3 | 1 | 3 | 2 | 3 | 124
3 | 3 | 2 | 2 | 1 | 125
1 | 2 | 2 | 1 | 3 | 126
1 | 2 | 3 | 2 | 3 | 127
1 | 1 | 2 | 2 | 3 | 123
(5 rows)
基本上我想要列名(a、b、c、d 或 e)在任何特定日期的值为“1”。
我猜想 pq.Array
给你一个字符串形式的 PostgreSQL 数组,所以你最终得到这样的东西:
unnest('{a,b,c,d,e}')
并且 PostgreSQL 不确定它应该如何解释该字符串,因此出现了关于 unnest(unknown)
的投诉。您应该能够添加显式类型转换来解决问题:
unnest(::text[]) -- PostgreSQL-specific casting syntax
unnest(cast( as text[])) -- Standard casting syntax
你最终会得到这样的结果:
rows, err := db.Query("select colname from (SELECT date, unnest(::text[]) AS colname, unnest() AS thing from test1 where date='123') as tester where thing=1;", pq.Array(arr1))