简体   繁体   English

如何使用SQL在列的列表中的key:value对中选择一个值?

[英]How do I select a value in a key:value pair within a list in a column using SQL?

In a table called payouts, there is a column stripeResponseData where the data is in the following structure: 在称为支付的表中,有一个stripeResponseData列,其中的数据具有以下结构:

{"id":"tr_1BlSHbGQXLV7RqqnHJffUVO0","object":"transfer","amount":39415,"amount_reversed":0,"balance_transaction":"txn_1BlSHbGQXfV7AqqnGi2o7UiY","created":1516239215,"currency":"usd","description":null,"destination":"acct_1BWWAmAzms5xPfV9","destination_payment":"py_1BlSHbAzms5xkfV91RHAOrno","livemode":true,"metadata":{},"reversals":{"object":"list","data":[],"has_more":false,"total_count":0,"url":"/v1/transfers/tr_1BlSHbYQXLV7AqqnHJffUVO0/reversals"},"reversed":false,"source_transaction":null,"source_type":"card","transfer_group":null}

Within my SQL SELECT statement, I want to return only the value of the key "destination". 在我的SQL SELECT语句中,我只想返回键“目标”的值。 How do I write my SQL query? 如何编写SQL查询?

My desired result of the query: 我期望的查询结果:

SELECT "stripeResponseData" FROM payouts [...]

(where I don't know how to write [...]) should look like the following (assume we have 3 rows with different values on "destination"): (我不知道如何写[...]的地方)应如下所示(假设我们有3行“目标”具有不同的值):

acct_1BWWAmAzms5xPfV9 acct_1BWWAmAzms5xPfV9

acct_1AY0phDc9pCDpLR8 acct_1AY0phDc9pCDpLR8

acct_1AwG3VL7DXxftOaS acct_1AwG3VL7DXxftOaS

How do I extract that value from the list within the stripeResponseData column? 如何从stripeResponseData列中的列表中提取该值?

See this sqlfiddle . 参见此sqlfiddle This query will fetch the ID from stripResponseData where the id is a specific id (Probably not very useful, but does show you how to select and query): 该查询将从stripResponseData中获取ID,其中id是特定的id(可能不是很有用,但确实向您展示了如何选择和查询):

SELECT data->>'id' FROM stripeResponseData WHERE data @> '{"id":"tr_1BlSHbGQXLV7RqqnHJffUVO0"}';

Because you mentioned your data was a string, you need to to type conversions to query/use it correctly. 因为您提到的数据是字符串,所以您需要键入转换才能正确查询/使用它。 See this sqlfiddle : 看到这个sqlfiddle

SELECT data::jsonb->>'id' FROM stripeResponseData WHERE data::jsonb @> '{"id":"tr_1BlSHbGQXLV7RqqnHJffUVO0"}';

Per your edit, you can simply query destination in almost the exact same way. 根据您的编辑,您可以简单地以几乎完全相同的方式查询目标位置。 This will get all the id's from stripeResponseData where destination = acct_1BWWAmAzms5xPfV9 : 这将从stripeResponseData where destination = acct_1BWWAmAzms5xPfV9获取所有id, stripeResponseData where destination = acct_1BWWAmAzms5xPfV9

SELECT data::jsonb->>'id' FROM stripeResponseData WHERE data::jsonb @> '{"destination":"acct_1BWWAmAzms5xPfV9"}';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从 SNOWFLAKE 的 SQL 表中的 json 字段中选择某些键/值对 - how do i select certain key/value pair from json field inside a SQL table in SNOWFLAKE 如何使用 SQL 将列/行对转换为键/值? - How to convert column/row pair to key/value by using SQL? SQL:我如何在 select 的所有列中至少有 2 个具有特定值的另一列? [使用 mariadb] - SQL: How do I select all of a column that have at least 2 of another column of a specific value? [using mariadb] SQL:如何设计版本化的键:值对,允许非唯一值? - SQL: How do I design a versioned key:value pair, allowing for non-unique values? 如何查询多个SQL表以查找特定的键值对? - how do I query multiple SQL tables for a specific key-value pair? 如何使用SQL从数据库中获取键值对 - How to get a key value pair from a Database using SQL 如何在 Spark SQL 中读取键值对? - How to read Key Value pair in spark SQL? 使用 SQL,如何根据行的内容向 select 的哪一列添加值? - Using SQL, how do I select which column to add a value to, based on the contents of the row? 如何使用sql函数在键值对中搜索特定键并返回其对应的值 - How to search for a particular key in a key value pair and return the corresponding value of it using sql function 如果列的值为 0,我如何选择表的行,否则如果 sql 中的值不为 0,则进行左连接? - How do i select rows of table if value of a column is 0 else do left join if value is not 0 in sql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM