以下代码能够正常运行。但我希望将数组 array['a', 'b', 'c', 'd', 'e'] 定义为一个变量。
但是出现了"pq: function unnest(unknown) is not unique"的错误。 表结构和样例数据--
基本上,我想要在特定日期找到具有值“1”的列名(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($1) AS colname, unnest($1) AS thing from test1 where date='123') as tester where thing=1;", pq.Array(arr1))
但是出现了"pq: function unnest(unknown) is not unique"的错误。 表结构和样例数据--
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)
基本上,我想要在特定日期找到具有值“1”的列名(a、b、c、d或e)。
postgresql
,但在c#中使用标准sql我会先创建一个字符数组,然后用逗号和空格分隔符将它们合并,在将其作为参数使用。 - Aleksa Ristic