【问题标题】:pq: function unnest(unknown) is not uniquepq: 函数 unnest(unknown) 不是唯一的
【发布时间】:2018-11-13 11:29:03
【问题描述】:

以下代码运行良好。但我想将 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($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 的专家,但在使用标准sql的c#中,我会创建字符数组,然后将它们与分隔符“,”合并,然后将其用作参数。
  • @AleksaRistic PostgreSQL 具有数组类型(AFAIK 是标准 SQL,尽管几乎没有人支持它们)。

标签: arrays postgresql go pq


【解决方案1】:

我猜pq.Array 会给你一个字符串形式的 PostgreSQL 数组,所以你最终会得到这样的结果:

unnest('{a,b,c,d,e}')

并且 PostgreSQL 不确定它应该如何解释该字符串,因此抱怨 unnest(unknown)。您应该能够添加显式类型转换来清除问题:

unnest($1::text[])         -- PostgreSQL-specific casting syntax
unnest(cast($1 as text[])) -- Standard casting syntax

你最终会得到这样的东西:

rows, err :=  db.Query("select colname from (SELECT date, unnest($1::text[]) AS colname, unnest($1) AS thing from test1 where date='123') as tester where thing=1;", pq.Array(arr1))

【讨论】:

  • 第二次嵌套中的变量替换发生错误。当我使用行时, err := db.Query("select colname from (SELECT date, unnest($1::text[]) AS colname, unnest(array[a, b, c, d, e]) AS thing from test1 where date='123') as tester where thing='1';", pq.Array(arr1)) 它给出了正确的输出。但是当使用以下代码时,我发现没有错误也没有输出。 rows, err := db.Query("select colname from (SELECT date, unnest($1::text[]) AS colname, unnest($1) AS thing from test1 where date='123') as tester where thing=1 ;", pq.Array(arr1))
  • 如果您在psql 中尝试查询会发生什么?
  • test=# select colname from (SELECT date, unnest(array['a', 'b', 'c', 'd', 'e']) AS colname, unnest(array[ a, b, c, d, e]) 来自 test1 的事物,其中 date='123') 作为测试者,其中事物='1'; colname --------- a b (2 行)
  • 它给出 2 个输出 a 和 b。
  • 您必须在问题中包含表结构和一些示例数据。
猜你喜欢
  • 1970-01-01
  • 2014-06-30
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 2015-02-06
相关资源
最近更新 更多