sql - Postgresql 中的 `->>` 和 `->` 有什么区别?

标签 sql json postgresql

->>->在SQL中有什么区别?

在本帖(Check if field exists in json type column postgresql)中,答主基本推荐使用,

json->'attribute' is not null

而不是,

json->>'attribute' is not null

为什么使用单箭头而不是双箭头?以我有限的经验,两者都做同样的事情。

最佳答案

-> 返回 json(或 jsonb),->> 返回 text:

with t (jo, ja) as (values
    ('{"a":"b"}'::jsonb,('[1,2]')::jsonb)
)
select
    pg_typeof(jo -> 'a'), pg_typeof(jo ->> 'a'),
    pg_typeof(ja -> 1), pg_typeof(ja ->> 1)
from t
;
 pg_typeof | pg_typeof | pg_typeof | pg_typeof 
-----------+-----------+-----------+-----------
 jsonb     | text      | jsonb     | text

关于sql - Postgresql 中的 `->>` 和 `->` 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38777535/

相关文章:

sql - 如何统计用户一天的标签数

postgresql - psql : FATAL: could not open file "base/11951/11717": Read-only file system

sql - 同一列的条件更新

mysql - SQL查询错误SELECT + JOIN +中的未知列

json - Elasticsearch 嵌套过滤器

Python:如何捕获异常并继续?

javascript - 限制 json 的结果

c# - EF code first many to many with payload, compound key or surrogate key

mysql - 如何在sql中每天一次执行触发器

c# - 在旧的 ascii postgresql 数据库中使用 npgsql entityFrameworkCore 的编码问题