我刚刚升级到Postgresql 9.3beta。当我将json_each或json_each_text函数应用于 json 列时,结果是一组列名为'key'和'value' 的行。
这是一个例子:
我有一个名为的表customers
,education
列的类型json
客户表如下:
----------------------------------------------------------------------
| id | first_name | last_name | education |
---- ------------ ----------- ----------------------------------------
| 1 | Harold | Finch | {\"school\":\"KSU\",\"state\":\"KS\"} |
----------------------------------------------------------------------
| 2 | John | Reese | {\"school\":\"NYSU\",\"state\":\"NY\"} |
----------------------------------------------------------------------
查询
select * from customers, json_each_text(customers.education) where value = 'NYSU'
返回一组具有以下列名称的行
---------------------------------------------------------------------------------------
| id | first_name | last_name | education | key | value |
---- ------------ ----------- ---------------------------------------- -------- -------
| 2 | John | Reese | {\"school\":\"NYSU\",\"state\":\"NY\"} | school | NYSU |
---------------------------------------------------------------------------------------
因为函数默认返回具有和列名json_each_text
的行集。key
value
但是,我想json_each_text
返回自定义列名,例如key1
and key2
:
-----------------------------------------------------------------------------------------
| id | first_name | last_name | education | key1 | value1 |
---- ------------ ----------- ---------------------------------------- -------- ---------
| 2 | John | Reese | {\"school\":\"NYSU\",\"state\":\"NY\"} | school | NYSU |
-----------------------------------------------------------------------------------------
应用这些函数后,有没有办法获得不同的列名,如“key1”和“value1” ?