英文:
How to retrieve the keys of the json of the first row in postgres in golang?
问题
我目前有一个像这样的表格:
id | value
----------
1 | {"key1":"value1", "test1":"value3"}
2 | {"key1":"value2", "test1":"value4"}
我希望返回的结果是:
key1, test1
每一行中的键都是相同的,但键的数量可能会改变。
我尝试使用以下语句:
SELECT jsonb_object_keys(value) FROM mn_statistics_company
然而,这会返回该行中所有 JSON 的所有键:
key1
test1
key1
test1
我还尝试了以下语句:
SELECT value FROM mn_statistics_company LIMIT 1
但这只返回带有键和值的 JSON:
{"key1":"value1", "test1":"value3"}
英文:
I currently have a table like this
id | value
----------
1 | {"key1":"value1", "test1":"value3"}
2 | {"key1":"value2", "test1":"value4"}
I would like this returned
key1, test1
The keys are the same in each row, but the number of keys may change.
I tired using
SELECT jsonb_object_keys(value) FROM mn_statistics_company
however, that got me all the keys of all the json of that row
key1
test1
key1
test1
and have tired
SELECT value FROM mn_statistics_company LIMIT 1
but that just returns the json with both the key and values.
{"key1":"value1", "test1":"value3"}
答案1
得分: 2
使用SELECT DISTINCT...
,例如:
SELECT DISTINCT jsonb_object_keys(value) FROM mn_statistics_company;
在SQLFiddle上查看示例。
英文:
Use SELECT DISTINCT...
, i.e.
SELECT DISTINCT jsonb_object_keys(value) FROM mn_statistics_company;
See example at SQLFiddle.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论