2016-07-11 11 views
0

私は基本的にjsonbタイプのカラムを持つpostgresqlテーブルを持っています。 JSONデータは、私はPostgreSQLのコマンドラインで「火星」と「テランの」国籍..を​​持っているすべての人々を取得したいこのpostgresqlのjsonbカラム内を検索するには

{ 
    "personal": { 
    { 
     "gender":"male", 
     "contact":{ 
      "home":{ 
      "email":"[email protected]", 
      "phone_number":"5551234" 
      }, 
      "work":{ 
      "email":"[email protected]", 
      "phone_number":"5551111" 
      } 
     }, 
     "religion":"other", 
     "languages":[ 
      "English", 
      "Xen" 
     ], 
     "last_name":"Eeo", 
     "birth_date":"1945-07-28", 
     "first_name":"Cee", 
     "nationality":"Martian", 
     "marital_status":"married" 
    } 
    } 
} 

のように見えるこのこの作品

select employees->'personal'->'contact'->'work'->'email' 
from employees 
where employees->'personal' @> '{"nationality":"Martian"}' 
    or employees->'personal' @> '{"nationality":"Terran"}' 

に動作します。..しかし、それは醜いだ..私はこのような何かを実行したいと思います:

select employees->'personal'->'contact'->'work'->'email' 
from employees 
where employees->'personal'->'nationality' in ('Martian','Terran') 

をしかし、私はこの1つのような書式設定のエラーを取得:

あなたはこれを実現するために、「テキストとして値を取得し、」オペレータ ->>使用する必要が0
DETAIL: Token "Martian" is invalid. 
CONTEXT: JSON data, line 1: Martian 

答えて

1

select employees->'personal'->'contact'->'work'->>'email' 
from employees 
where employees->'personal'->>'nationality' in ('Martian','Terran') 

はまた、私はあなたがテキストとしてそれをしたいと仮定するので、電子メールを取得し、それを追加しました。

(employees->'personal'->'nationality')::textのテキストへのキャストは、値が返されず、テキストに変換されたjson(この場合は引用符を含む"Martian")を返すため、機能しません。

+0

新しい質問(関連)http://stackoverflow.com/questions/38324360/how-to-use-postgresql-any-with-jsonb-data – abbood

1

使用->> operator

select employees->'personal'->'contact'->'work'->'email' 
from employees 
where employees->'personal'->>'nationality' in ('Martian','Terran') 
関連する問題