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 JSONB数组_Postgresql_Activeadmin_Jsonb - Fatal编程技术网

Postgresql 如何使用=运算符查询Postgres JSONB数组

Postgresql 如何使用=运算符查询Postgres JSONB数组,postgresql,activeadmin,jsonb,Postgresql,Activeadmin,Jsonb,我正在寻找一种使用kind of=子句查询PostgreSQL JSONB数组字段的方法 让我们假设我有一张桌子 CREATE TABLE events( id integer, tags jsonb, PRIMARY KEY(id) ); 具有值的标记,如['Google'、'Hello World'、'Ruby'] 我经历过并做过类似的事情 形成的SQL如下所示 SELECT "events".* FROM "events" WHE

我正在寻找一种使用kind of=子句查询PostgreSQL JSONB数组字段的方法

让我们假设我有一张桌子

CREATE TABLE events(
   id integer,
   tags jsonb,
   PRIMARY KEY(id)
);
具有值的标记,如
['Google'、'Hello World'、'Ruby']

我经历过并做过类似的事情

形成的SQL如下所示

SELECT "events".* FROM "events" WHERE ("events"."tags" @> '{google}')  ORDER BY "business_events"."id" desc;
通过运行这个,我得到了这个错误=>

ERROR:  invalid input syntax for type json
LINE 1: ...siness_events" WHERE ("business_events"."tags" @> '{google}'...
                                                             ^
DETAIL:  Token "google" is invalid.
CONTEXT:  JSON data, line 1: {google...
有什么想法吗?

的右操作数应该是有效的
json

WITH events(id, tags) AS (
VALUES
    (1, '["Google", "Hello World", "Ruby"]'::jsonb)
)

SELECT events.* 
FROM events 
WHERE tags @> '["Google"]'

 id |               tags                
----+-----------------------------------
  1 | ["Google", "Hello World", "Ruby"]
(1 row)
请注意,json对象的键和文本值用双引号括起来

运算符按原样接受参数,无法使其不区分大小写。您可以使用函数
jsonb\u array\u elements\u text()
完成以下操作:

SELECT events.*
FROM events 
CROSS JOIN jsonb_array_elements_text(tags)
WHERE lower(value) = 'google';

第二种解决方案要昂贵得多,这里的注释也适用。

如何使其不区分大小写?有什么想法吗?