【问题标题】:What is the difference between `->>` and `->` in Postgres SQL?Postgresql中的`->>`和`->`有什么区别?
【发布时间】:2016-12-11 04:17:16
【问题描述】:

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

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

json->'attribute' is not null

而不是,

json->>'attribute' is not null

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

【问题讨论】:

标签: sql json postgresql


【解决方案1】:

-> 返回 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

【讨论】:

  • 您的意思可能是第一个运算符返回jsonb(而不是json(b))。
  • @AlexanderFarber 我的意思是它可以返回 json 和 jsonb 因此括号
【解决方案2】:

PostgreSQL 提供了两个原生运算符->->> 来帮助您查询JSON 数据。

运算符 -> 将 JSON 对象字段作为 JSON 返回。 运算符->> 以文本形式返回 JSON 对象字段。

以下查询使用运算符 -> 以 JSON 形式获取所有客户:

SELECT
 info -> 'customer' AS customer
FROM
 orders;
customer
--------
"John Doe"
"Lily Bush"
"Josh William"
"Mary Clark"

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

SELECT
 info ->> 'customer' AS customer
FROM
 orders;
customer
--------
John Doe
Lily Bush
Josh William
Mary Clark

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

【讨论】:

【解决方案3】:

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

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

我们还必须了解,我们现在有 2 种不同的 null

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

我在jsfiddle上创建了一个示例

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

create table json_test (
  id integer,
  val JSONB
);

并插入一些测试数据:

INSERT INTO json_test (id, val) values
(1, jsonb_build_object('member', null)),
(2, jsonb_build_object('member', 12)),
(3, null);

我们在 sqlfiddle 中看到的输出:

id  | val
----+-----------------
 1  | {"member": null}
 2  | {"member": 12}
 3  | (null)

注意事项:

  1. 包含一个 JSONB 对象,并且唯一的字段 membernull
  2. 包含一个 JSONB 对象,并且唯一的字段 member 具有数值 12
  3. (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;

输出:

+----+--------+---------------+---------------+-----------+-------------------+-------------------+--------------+
| 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        |
+----+--------+---------------+---------------+-----------+-------------------+-------------------+--------------+

注意事项:

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

【讨论】:

    猜你喜欢
    • 2010-11-15
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多