Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql Postgres:检查数组字段是否包含值?_Postgresql - Fatal编程技术网

Postgresql Postgres:检查数组字段是否包含值?

Postgresql Postgres:检查数组字段是否包含值?,postgresql,Postgresql,我确信这是一个重复的问题,因为答案就在某个地方,但我在谷歌搜索了10分钟后还没有找到答案,所以我呼吁编辑们不要关闭它,因为它可能对其他人有用 我用的是Postgres9.5。这是我的桌子: Column │ Type │ Modifiers ─────────────────────────┼───────────────────────────┼──────

我确信这是一个重复的问题,因为答案就在某个地方,但我在谷歌搜索了10分钟后还没有找到答案,所以我呼吁编辑们不要关闭它,因为它可能对其他人有用

我用的是Postgres9.5。这是我的桌子:

        Column          │           Type            │                                Modifiers
─────────────────────────┼───────────────────────────┼─────────────────────────────────────────────────────────────────────────
 id                      │ integer                   │ not null default nextval('mytable_id_seq'::regclass)
 pmid                    │ character varying(200)    │
 pub_types               │ character varying(2000)[] │ not null
我想在pub_类型中查找包含日记账的所有行

我已经找到了文档并用谷歌搜索了一下,这就是我尝试过的:

select * from mytable where ("Journal") IN pub_types;
select * from mytable where "Journal" IN pub_types;
select * from mytable where pub_types=ANY("Journal");
select * from mytable where pub_types IN ("Journal");
select * from mytable where where pub_types contains "Journal";
我已经扫描过了,但看不到一个简单的如何运行查询的示例,而StackOverflow问题似乎都是基于更复杂的示例

这应该有效:

select * from mytable where 'Journal'=ANY(pub_types);
i、 e.语法为=ANY。还要注意,postresql中的字符串文字是用单引号写的。

这对我来说很有用:

select * from mytable
where array_to_string(pub_types, ',') like '%Journal%'
根据您的规范化需求,最好使用FK引用实现一个单独的表,因为这样可以获得更好的性能和可管理性。

对于任何操作符,您只能搜索一个值

比如说,

select * from mytable where 'Book' = ANY(pub_types);
select * from mytable where pub_types @> '{"Journal", "Book"}';
如果要搜索多个值,可以使用@>运算符

比如说,

select * from mytable where 'Book' = ANY(pub_types);
select * from mytable where pub_types @> '{"Journal", "Book"}';
您可以指定您喜欢的顺序。

我们可以使用任意数组来转换为枚举数组,例如:

create type example_enum as enum (
  'ENUM1', 'ENUM2'
);

create table example_table (
  id integer,
  enum_field example_enum
);

select 
  * 
from 
  example_table t
where
  t.enum_field = any(array['ENUM1', 'ENUM2']::example_enum[]);
或者我们仍然可以使用“IN”子句,但首先,我们应该“unest”它:

select 
  * 
from 
  example_table t
where
  t.enum_field in (select unnest(array['ENUM1', 'ENUM2']::example_enum[]));
示例:

这对我很有效

let exampleArray = [1, 2, 3, 4, 5];
let exampleToString = exampleArray.toString(); //convert to toString
let query = `Select * from table_name where column_name in (${exampleToString})`; //Execute the query to get response

我遇到了同样的问题,经过一个小时的努力,我知道不应该在查询中直接访问数组。然后我发现数据应该在它自己的数据库中发送,然后我再次使用js中的toString方法将该数组转换为字符串。因此,我通过执行上述查询进行了工作,并得到了预期的结果

@redneb如果我想检查数组字段是否包含数组中的项,该怎么办?我得到了一个错误:匿名复合类型的输入没有实现。这与in相同?in需要一个显式的值列表或子查询,而任何类型都可以使用数组。如果数组中已有值列表,例如,当数组存储在数据库的某个列中时(如OP的情况),这可能很有用。关于性能的这种语法如何?从mytable中选择*,其中pub_types@>array['Journal'::text];与标记类似,如果您不打算保留标记表或只有一个实体使用它们,则会产生误报。如果您有多个前缀相同的值,则会产生误报,即日记条目OP对问题的措辞方式似乎希望在字符串中的任何位置找到日记。如果您只想匹配单词日志的具体位置,只需删除前导和尾随的通配符,即%.Nice-使我能够对数组执行类似ILIKE的查询;非常感谢。从存档中选择*从数组到字符串,如“%pLASt%”;@>means包含该数组中的所有值。如果要搜索当前数组是否包含其他数组中的任何值,可以使用&&。从mytable中选择*,其中pub_类型为&&“{Journal,Book}”;我不知道这是否是一个版本,但@>和&&在Postgres 9.6上对我的作用完全相同。他们两人都匹配列表中的任何项目。除了@>还匹配了一个空列表“{}”。请给代码添加一个解释。这将提高你的答案质量。对于PostgreSQL,字符串使用单引号。双引号用于分隔符名称,例如列、索引等。