使用MySQL 5.7,我想知道PostgreSQL相当于
SELECT * FROM user_conversations WHERE JSON_CONTAINS(users, JSON_ARRAY(1))
如何在PostgreSQL中编写JSON_CONTAINS(users, JSON_ARRAY(1))
编辑1
有我的json它只是一个没有son对象的数组:
[
"john",
"doe",
"does"
]
我希望得到“doe”例如
编辑2
我的表:
Column | Type | Modifiers | Storage | Statistics Target | Description
------------+--------------------------------+-------------------------------------------------------+----------+-----------------------+-------------
id | uuid | non NULL | plain | |
name | character varying(255) | non NULL | extended | |
image | character varying(255) | non NULL Par défaut, 'default.png'::character varying | extended | |
users | json | non NULL | extended | |
type | integer | non NULL | plain | |
emoji_id | integer | non NULL Par défaut, 0 | plain | |
created_at | timestamp(0) without time zone | | plain | |
updated_at | timestamp(0) without time zone |
编辑3: 我使用laravel来执行查询:
DB::table('user_conversations')->whereRaw('JSON_CONTAINS(users, JSON_ARRAY(1))')->orderBy('created_at', 'desc')->paginate(5);
并且可以使用mysql。
答案 0 :(得分:1)
您引用的MySQL的JSON_CONTAINS()
的两个参数形式具有JSON_CONTAINS(json_doc, val)
的签名听起来类似于PostgreSQL JSON operator @>
运算符
-- returns true
SELECT '["john","bob","doe","dylan","mike","does"]'::jsonb @>
'["john","doe","does"]';
-- returns false
SELECT '["john","bob","doe","dylan","mike","does"]'::jsonb @>
'["john","mary"]';
如果您的类型是json,那么将其强制转换为jsonb
;
SELECT *
FROM user_conversations
WHERE users::jsonb @> json_text;