Postgres SQL中` - >>`和` - >`有什么区别?

tim*_*xyz 41 sql postgresql json

->>->SQL 之间有什么区别?

在这个线程中(检查json类型列postgresql中是否存在字段),回答者基本上建议使用,

json->'attribute' is not null
Run Code Online (Sandbox Code Playgroud)

代替,

json->>'attribute' is not null
Run Code Online (Sandbox Code Playgroud)

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

Clo*_*eto 30

->返回json(b)->>返回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
Run Code Online (Sandbox Code Playgroud)

  • @AlexanderFarber我的意思是它可以返回json和jsonb因此括号 (4认同)
  • 我澄清了“json(b)”措辞 (3认同)
  • 您可能的意思是第一个运算符返回“jsonb”(而不是“json(b)”)。 (2认同)

Edd*_*nne 14

PostgreSQL提供了两个本机运算符->,->>可帮助您查询JSON数据.

运算符->将JSON对象字段作为JSON返回.运算符->>将JSON对象字段作为文本返回.

以下查询使用operator ->以JSON的形式获取所有客户:

在此输入图像描述

以下查询使用operator ->>以文本形式获取所有客户:

在此输入图像描述

您可以在以下链接中查看更多详细信息 :http://www.postgresqltutorial.com/postgresql-json/

  • 请[不要将代码作为图像发布](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-问题/ 285557) (2认同)

Mic*_*ott 14

单箭头->用于直接访问 JSON

双箭头->>用于访问转换.

(正如这里已经回答过几次一样。)

记住这一点的关键是:

  • ->较短箭头用于减少工作量:它只做一件事: 一个箭头,一件事:仅访问 JSON
  • ->>较长箭头需要您输入更多工作,并且 postgres 需要做更多工作:两个箭头,两件事:访问 JSON并将其转换为文本


TmT*_*ron 10

Postgres 提供 2 个运算符来获取 JSON 成员:

  • 箭头运算符:->返回类型 JSON 或 JSONB
  • 双箭头运算符:->>返回类型文本

我们还必须明白,我们现在有两种不同的null

  • (null) postgres null 类型
  • null json/b 空类型

我在jsfiddle上创建了一个例子

让我们创建一个带有 JSONB 字段的简单表:

create table json_test (
  id integer,
  val JSONB
);
Run Code Online (Sandbox Code Playgroud)

并插入一些测试数据:

INSERT INTO json_test (id, val) values
(1, jsonb_build_object('member', null)),
(2, jsonb_build_object('member', 12)),
(3, null);
Run Code Online (Sandbox Code Playgroud)

我们在 sqlfiddle 中看到的输出:

id  | val
----+-----------------
 1  | {"member": null}
 2  | {"member": 12}
 3  | (null)
Run Code Online (Sandbox Code Playgroud)

笔记:

  1. 包含一个 JSONB 对象并且唯一的字段membernull
  2. 包含一个 JSONB 对象并且唯一的字段member具有数值12
  3. is (null) : 即整个列是(null)并且根本不包含 JSONB 对象

为了更好地理解差异,让我们看一下类型和空检查:

SELECT id,
  val -> 'member'  as arrow,
  pg_typeof(val -> 'member')  as arrow_pg_type,
  val -> 'member' IS NULL as arrow_is_null,
  val ->> 'member' as dbl_arrow,
  pg_typeof(val ->> 'member')  as dbl_arrow_pg_type,
  val ->> 'member' IS NULL as dbl_arrow_is_null,
  CASE WHEN jsonb_typeof(val -> 'member') = 'null' THEN true ELSE false END as is_json_null
from json_test;
Run Code Online (Sandbox Code Playgroud)

输出:

+----+--------+---------------+---------------+-----------+-------------------+-------------------+--------------+
| id | arrow  | arrow_pg_type | arrow_is_null | dbl_arrow | dbl_arrow_pg_type | dbl_arrow_is_null | is_json_null |
+----+--------+---------------+---------------+-----------+-------------------+-------------------+--------------+
| 1  | null   | jsonb         | false         | (null)    | text              | true              | true         |
+----+--------+---------------+---------------+-----------+-------------------+-------------------+--------------+
| 2  | 12     | jsonb         | false         | 12        | text              | false             | false        |
+----+--------+---------------+---------------+-----------+-------------------+-------------------+--------------+
| 3  | (null) | jsonb         | true          | (null)    | text              | true              | false        |
+----+--------+---------------+---------------+-----------+-------------------+-------------------+--------------+
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 对于{"member": null}
    • val -> 'member' IS NULL假的
    • val ->> 'member' IS NULL 是真的
  • is_json_null可用于获取 json- null条件