[英]PostgreSQL query rows in json object that contains certain key value
I have this sample table called tx
that stores information about transactions, and I am using PostgreSQL 10.6.我有一个名为tx
示例表,用于存储有关事务的信息,并且我使用的是 PostgreSQL 10.6。
# info about my PostgreSQL version
select version()
> PostgreSQL 10.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11), 64-bit
It looks like this:它看起来像这样:
# create table
create table tx (id bigserial primary key, msg jsonb not null);
# create index on 'type' value of msg column
create index on tx using gin ((msg->'type'));
Insert rows into tx
table在tx
表中插入行
insert into tx (msg) values
('[{"type": "MsgSend", "value": {"amount": [{"denom": "dollar", "amount": "100"}], "from": "Jeniffer", "to": "James" }}]'),
('[{"type": "MsgSend", "value": {"amount": [{"denom": "dollar", "amount": "30"}], "from": "Jeniffer", "to": "James" }}]'),
('[{"type": "MsgBuy", "value": {"amount": [{"denom": "dollar", "amount": "10"}], "from": "George", "to": "Smith" }}]'),
('[{"type": "MsgSend", "value": {"amount": [{"denom": "dollar", "amount": "60"}], "from": "Jeniffer", "to": "James" }}]');
I have read this Querying JSON (JSONB) data types in PostgreSQL and searched similar posts and tested with any given examples, but they don't seem to guide me well to solve what I am trying to accomplish, which is to query rows in json object
, not json array
我已经在 PostgreSQL 中阅读了这个查询 JSON (JSONB) 数据类型并搜索了类似的帖子并使用任何给定的示例进行了测试,但它们似乎并没有很好地指导我解决我想要完成的任务,即在json object
查询行json object
,而不是json array
These posts that这些帖子
Check if a Postgres JSON array contains a string 检查 Postgres JSON 数组是否包含字符串
querying json object from table in postgreSQL 从 postgreSQL 表中查询 json 对象
PostgreSQL JSON Query with embedded json Object 带有嵌入式 json 对象的 PostgreSQL JSON 查询
How do I go about achieving these queries?我如何去实现这些查询? I believe if I were to know how to query one, then I would be able to solve the other questions.我相信如果我知道如何查询一个,那么我就能解决其他问题。
How do I query data where column msg
contains where key value of type
is MsgSend
?如何查询msg
列包含type
键值为MsgSend
?
How do I query data where column msg
contains where key value of from
is Jeniffer
?如何查询msg
列包含的数据,其中from
键值是Jeniffer
?
How do I query data where column msg
contains MsgSend
and amount is greater than 50
?如何查询msg
列包含MsgSend
且数量greater than 50
?
I will be providing any information that may be needed to figure out this question.我将提供解决此问题可能需要的任何信息。
The official Postgresql documentation contains all you need.官方Postgresql 文档包含您需要的一切。
See below queries per your questions:根据您的问题查看以下查询:
- How do I query data where column msg contains where key value of type is MsgSend?如何查询 msg 列包含类型键值为 MsgSend 的数据?
select * from tx where msg->0->>'type' = 'MsgSend';
- How do I query data where column msg contains where key value of from is Jeniffer?如何查询 msg 列包含的数据,其中 from 的键值是 Jeniffer?
select * from tx where msg->0->'value'->>'from' = 'Jeniffer';
- How do I query data where column msg contains MsgSend and amount is greater than 50?如何查询 msg 列包含 MsgSend 且数量大于 50 的数据?
select * from tx where (msg->0->'value'->'amount'->0->>'amount')::int > 50;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.