简体   繁体   English

PostgreSQL 查询包含特定键值的 json 对象中的行

[英]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 tabletx表中插入行

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这些帖子

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.我相信如果我知道如何查询一个,那么我就能解决其他问题。

  1. How do I query data where column msg contains where key value of type is MsgSend ?如何查询msg列包含type键值为MsgSend

  2. How do I query data where column msg contains where key value of from is Jeniffer ?如何查询msg列包含的数据,其中from键值是Jeniffer

  3. 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:根据您的问题查看以下查询:

  1. 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';
  1. 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';
  1. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM