2016-08-04 28 views
12

SQL'de ->> ve -> arasındaki fark nedir? Bu konuya (Check if field exists in json type column postgresql), answerer temelde kullanılmasını önerir yılında Postgres SQL'de `- >>` ve `->` arasındaki fark nedir?

yerine

json->'attribute' is not null 

,

json->>'attribute' is not null 

Neden çift ok yerine tek oku kullanın? Benim sınırlı tecrübemde, ikisi de aynı şeyi yapıyor.

+4

[İnce El Kitabının okunmasının unutulmuş erdemi.] (Https://www.postgresql.org/docs/current/static/functions-json.html) –

cevap

8

-> döner json(b) ve ->> döner 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

Muhtemelen ilk operatörün jsonb'yi döndürdüğü anlamına gelir. (ve json (b) 'değil). –

+2

@AlexanderFarber Yani hem json hem de jsonb parantezini döndürebilir yani parantez –

2

PostgreSQL JSON verilerini sorgulamak yardımcı olmak üzere iki yerli operatörleri -> ve ->> sağlar.

Operatör ->, JSON nesne alanını JSON olarak döndürür. Operatör ->>, JSON nesne alanını metin olarak döndürür.

aşağıdaki sorgu JSON biçiminde tüm müşteri elde operatörünü -> kullanır:

enter image description here

Ve aşağıdaki sorgu metni şeklinde tüm müşteri elde operatörünü ->> kullanır:

enter image description here

Aşağıdaki linkten http://www.postgresqltutorial.com/postgresql-json/

+0

Lütfen [resim olarak kod yazmayın] (http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload- images-of-kod-on-so-zaman-sorma-a-soru/285557) –