2016-08-04 309 views
12

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

在这个线程(Check if field exists in json type column postgresql),回答者基本上推荐使用,

json->'attribute' is not null 

代替,

json->>'attribute' is not null 

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

+4

[热烈祝贺阅读精细手册被遗忘的美德。](https://www.postgresql.org/docs/current/static/functions-json.html) –

回答

8

->回报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 
+0

您可能意指第一个运算符返回'jsonb' (而不是'json(b)')。 –

+2

@AlexanderFarber我的意思是它可以返回json和jsonb因此括号 –

2

PostgreSQL提供了两种本地运营商->->>帮您查询JSON数据。

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

下面的查询使用运营商->让所有客户在JSON形式:

enter image description here

而下面的查询使用运营商->>让所有客户以文本形式:

enter image description here

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

+0

请不要将代码作为图像发布(http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-图像 - 的代码上那么当-要价-A-问题/ 285557) –

相关问题