如果你想檢查是否在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)
大,三江源 – puneet