- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚升级到 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
和 key2
:
-----------------------------------------------------------------------------------------
| id | first_name | last_name | education | key1 | value1 |
---- ------------ ----------- ---------------------------------------- -------- ---------
| 2 | John | Reese | {\"school\":\"NYSU\",\"state\":\"NY\"} | school | NYSU |
-----------------------------------------------------------------------------------------
有没有办法在应用这些函数后获得不同的列名,如 'key1' 和 'value1'?
最佳答案
您可以通过在 FROM 和 SELECT 子句中使用 AS 来解决这个问题:
postgres=# SELECT json_data.key AS key1,
json_data.value AS value1
FROM customers,
json_each_text(customers.education) AS json_data
WHERE value = 'NYSU';
key1 | value1
--------+--------
school | NYSU
(1 row)
关于postgresql - json_each 和 json_each_text 结果具有不同的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16650796/
我收到 json_each 函数不存在的错误。我正在使用 postgresql 9.3。我不知道怎么了。请在这里帮助我。 select * from json_each(( select ed
我有以前使用 json_each(x) 从 Postgresql 中提取的数据,我正在数据库迁移后在 Presto 中寻找 json_each 的等效项以将我的数据提取到多行中。原来的格式是这样的{"
我的字段中没有 json 文本。我有一行包含 "text","format","keyname","..." 列,我想以 '{"value of keyname 格式将其导出到 json列”:{“文本
我刚刚升级到 Postgresql 9.3beta。当我将 json_each 或 json_each_text 函数应用于 json 列时,结果是一组列名称为 'key' 和 的行“值(value)
我正在尝试使用 Postgres 生成以下输出 ONE TWO THREE ===================== A1 A2 A3 B1 B2
我正在尝试找出使用 json_each 的正确方法。我见过一些技巧,例如使用 column 或 text。到目前为止,我已经找到了一种使用 table_valued 的非常干净的方法,除了交叉连接警告
我已经通过 brew 安装了带有 JSON1 的 SQLite3: brew install sqlite3 --with-json1 --with-fts5 版本: 3.15.2 2016-11-2
我是一名优秀的程序员,十分优秀!