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)
Edd*_*nne 14
PostgreSQL提供了两个本机运算符->,->>可帮助您查询JSON数据.
运算符->将JSON对象字段作为JSON返回.运算符->>将JSON对象字段作为文本返回.
以下查询使用operator ->以JSON的形式获取所有客户:
以下查询使用operator ->>以文本形式获取所有客户:
您可以在以下链接中查看更多详细信息 :http://www.postgresqltutorial.com/postgresql-json/
Mic*_*ott 14
单箭头->用于直接访问 JSON。
双箭头->>用于访问和转换.
(正如这里已经回答过几次一样。)
记住这一点的关键是:
->较短的箭头用于减少工作量:它只做一件事: 一个箭头,一件事:仅访问 JSON->>较长的箭头需要您输入更多工作,并且 postgres 需要做更多工作:两个箭头,两件事:访问 JSON并将其转换为文本TmT*_*ron 10
Postgres 提供 2 个运算符来获取 JSON 成员:
->返回类型 JSON 或 JSONB->>返回类型文本我们还必须明白,我们现在有两种不同的null:
我在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)
笔记:
member为nullmember具有数值12为了更好地理解差异,让我们看一下类型和空检查:
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条件| 归档时间: |
|
| 查看次数: |
14571 次 |
| 最近记录: |