[英]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.