2015-04-24 183 views
1

我想在表格中的json對象的postgreSql中使用where條件。我需要這樣做,例如:我有一個表'測試'它有三列名稱(varchar),url(varchar),更多(json)。我需要檢索css21Colors = Purple的日期。 more是一個json類型,下面是更多字段的值。從postgreSQL查詢json對象

請讓我知道同樣的查詢語法應該是什麼?

more = {"colorTree":{"Purple":[{"Spanish Violet":"#522173"}], 
"Brown":[{"Dark Puce":"#4e3347"}],"White":[{"White":"#ffffff"}], 
"Black":[{"Eerie Black":"#1d0d27"}],"Gray":[{"Rose Quartz":"#a091a4"}]}, 
"sizeoutscount":0,"css21Colors":{"Purple":69,"Brown":5,"White":4,"Black":17,"Gray":3}, 
"sizeins": [],"sizeinscount":0,"sizeouts":[],"allsizes":["8","10","16"], 
"css3Colors": {"Rose Quartz":3,"White":4,"Dark Puce":5,"Eerie Black":17,"Spanish 
Violet":69},"hexColors":{"#522173":69,"#4e3347":5,"#ffffff":4,"#1d0d27":17,"#a091a4":3}} 
+1

提及PostgreSQL版本('select version()') –

+0

PostgreSQL版本是9.4.0 – singh

回答

1
SELECT more->'css21Colors'->'Purple' FROM test; 

此外,您可以查詢只包含關鍵的行。

SELECT 
    more->'css21Colors'->'Purple' 
FROM 
    test 
WHERE 
    (more->'css21Colors')::jsonb ? 'Purple'; 

注意切換到jsonb數據類型。

+0

謝謝,它工作。 – singh