[英]query a json key in postgres json field
Say I have in Postgres stored in JSON field called “data” like this 假设我在Postgres中存储了名为“data”的JSON字段,就像这样
{
"CUSTA": {
"name": "Customer A",
},
"CUSTB": {
"name": "Customer B",
},
"CUSTC": {
"name": "Customer C",
}
}
How can I query to return the record that contains the key “CUSTA” ? 如何查询返回包含密钥“CUSTA”的记录? or even better the value of “CUSTA” which is "name": "Customer A"
甚至更好的“CUSTA”的价值是“名称”:“客户A”
trying to do something like this but obviously i cant use the keyword key 试图做这样的事情,但显然我不能使用关键字键
SELECT * FROM invoices WHERE data->>key = 'CUSTA';
select '{
"CUSTA": {
"name": "Customer A"
},
"CUSTB": {
"name": "Customer B"
},
"CUSTC": {
"name": "Customer C"
}
}'::json#>>'{CUSTA}';
?column?
------------------------------
{ +
"name": "Customer A"+
}
(1 row)
note: you have trailing commas after name:customer x, which is not proper json. 注意:在名称后面有逗号逗号:customer x,这不是正确的json。 For your query, you would probably do something like:
对于您的查询,您可能会执行以下操作:
select data#>>'{CUSTA}' from invoices;
or, if data isn't already a json field: 或者,如果数据不是json字段:
select data::json#>>'{CUSTA}' from invoices;
I don't understand why any invoice would have more than one customer though. 我不明白为什么任何发票都会有多个客户。
-g -G
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.