2017-02-19 19 views
0

私は次のJSON形式をpostgresqlのJSONB列として格納しています。PostgreSQLの配列値のクエリ

{DisplayName":"Bob marley","FName":"Bob","Title":null,"LName":"Marley","State":null,"EmailAddresses":["[email protected]","[email protected]"]}, 
{DisplayName":"Bob martin","FName":"Bob ","Title":null,"LName":"Martin","State":null,"EmailAddresses":["[email protected]","[email protected]"]} 

私はEmailAddresses Arrayに対して、Bobで始まる場所を検索します。私は、通常の文字列型キーをLIKEとILIKE演算子を使って照会することができますが、ARRAY型を検索するには問題があります。 お知らせください。

答えて

1

あなたがマスクにjsonbフィールドの試合で配列の任意の要素かどうかを確認したい場合:

SELECT * 
    FROM jtable 
WHERE EXISTS (
     SELECT 1 
      FROM jsonb_array_elements_text(jfield->'EmailAddresses') AS j 
     WHERE j ~ 'bob' 
    ) 

                       jfield                     
---------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
{"FName": "Bob", "LName": "Marley", "State": null, "Title": null, "DisplayName": "Bob marley", "EmailAddresses": ["[email protected]", "[email protected]"]} 
{"FName": "Bob ", "LName": "Martin", "State": null, "Title": null, "DisplayName": "Bob martin", "EmailAddresses": ["[email protected]", "[email protected]"]} 
(2 rows) 

それとも、マスクに一致する電子メールのリストをしたい場合:

WITH w AS (
    SELECT jsonb_array_elements_text(jfield->'EmailAddresses') AS emails, 
      * 
     FROM jtable 
) 
SELECT * FROM w WHERE emails ~ 'bobm' 


     emails  |                    jfield                     
---------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
[email protected] | {"FName": "Bob", "LName": "Marley", "State": null, "Title": null, "DisplayName": "Bob marley", "EmailAddresses": ["[email protected]", "[email protected]"]} 
[email protected] | {"FName": "Bob ", "LName": "Martin", "State": null, "Title": null, "DisplayName": "Bob martin", "EmailAddresses": ["[email protected]", "[email protected]"]} 
(2 rows) 
+0

グレート、ありがとうを – puneet

関連する問題