sql - 如何以 JSON 形式选择表的列名?
问题描述
我正在尝试以 JSON 格式返回表的列名,其中每个列名是键,数据类型是值。
我有以下代码:
SELECT jsonb_agg(json_build_object(column_name, udt_name::regtype)) AS list
FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'layer_1001'
这会产生以下结果:
[{"id": "integer"}, {"geom": "geometry"}, {"address": "text"}, {"start_date": "timestamp without time zone"}]
但我需要代码产生以下结果:
{"id": "integer", "geom": "geometry", "address": "text", "start_date": "timestamp without time zone"}
有人知道该怎么做吗?
解决方案
它们接受两个参数,键和值的数组,并且都必须可序列化为文本
SELECT
JSON_OBJECT(ARRAY_AGG(column_name::TEXT), ARRAY_AGG(udt_name::text))
FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'mytable'
推荐阅读
- flutter - Flutter TabController - 在任何特定的 tabitem 内推送屏幕
- javascript - [Vue 警告]:v-on 处理程序中的错误:“TypeError:无法读取未定义的属性 'scrollLeft'”
- html - 如何让光标在完成第二行时闪烁 - 使用 CSS 的打字效果
- java - Swing JFrame setSize() 实际大小
- javascript - 单击 Django 站点的图标时,Jquery/Ajax 功能不起作用
- algorithm - 为什么用大 O 表示法而不是 Theta 给出算法复杂度?
- c# - 如何在单个网站中运行多个 Blazor 应用程序
- cordova - 我可以在没有电容器的情况下使用 Ionic 5 吗?我应该只使用 Quasar 吗?
- tensorflow - EfficientDet D0 512x512 图像尺寸
- in-app-purchase - Purchase.wasParchased 退货属于 Google 内部测试