我在postgresql中有一个array_agg列,它的值如下:
"{Metabolic/Endocrinology}"
"{Cardiovascular}"
"{Oncology}"
"{Autoimmune/Inflammation}"基本上是一个字符串变量,由一个id进行array_agg。
现在我想从这个表中获取所有存在肿瘤学或自身免疫/炎症的记录。
我正在做这样的事情,但我不确定它为什么会抛出错误。
select * from Table where id = ANY('{Oncology,Autoimmune/Inflammation}')它抛出以下错误。
ERROR: operator does not exist: text[] = text
SQL state: 42883
Hint: No operator matches the given name and argument type(s). You may need to add explicit type casts.
Character: 67请注意,我也使用了::TEXT [],但它仍然给出一个错误。
发布于 2015-10-03 18:13:26
您希望使用数组重叠运算符&&。
参见array operators。
例如:
select * from (
VALUES
(ARRAY['Oncology','Pediatrics']),
(ARRAY['Autoimmune/Inflammation','Oncology']),
(ARRAY['Autoimmune/Inflammation']),
(ARRAY['Pediatrics']),
(ARRAY[]::text[])
) "Table"(id)
where id && ARRAY['Oncology','Autoimmune/Inflammation'];顺便说一句,我建议尽可能使用SQL标准的ARRAY[...]构造函数。
此外,几乎可以肯定的是,将id列(假设是primary key,如果不是,其名称会令人困惑)定义为数组类型是一个糟糕的想法。
发布于 2015-10-03 08:45:00
由于id的类型为text[],因此此查询应该有效:
select * from Table where id[1] = ANY('{Oncology,Autoimmune/Inflammation}')但是,只有当id始终是单元素数组时,这才会给出预期的结果。
https://stackoverflow.com/questions/32917346
复制相似问题