简体   繁体   English

在 Presto Athena 中将字符串转换为数组

[英]Convert String to Array in Presto Athena

I am new in SQL. I am trying to pass a list in WHERE IN statement.我是 SQL 的新人。我正在尝试在 WHERE IN 语句中传递一个列表。 But the values are in a string similar to this:但这些值位于类似于以下的字符串中:

'["id1", "id2", "id3", "id4", "id5", "id6"]'

The WHERE IN statement do not accept string but accept it: WHERE IN 语句不接受字符串但接受它:

WHERE IN (SELECT ids FROM unnested_id_related.id_related)

I can get this table using UNNEST statement我可以使用 UNNEST 语句获取此表

CROSS JOIN 
    unnest(ids) AS unnested_id_related (id_related)

But the value ids needs to be an ARRAY.但是值ids需要是一个数组。 Anyone knows how can I convert String to Array?任何人都知道如何将字符串转换为数组? Or a better way to pass this string in the WHERE IN statement?还是在 WHERE IN 语句中传递此字符串的更好方法?

Your data looks like json array, so you canparse it as one and cast to array of strings:您的数据看起来像 json 数组,因此您可以将其解析为一个并转换为字符串数组:

select cast(json_parse('["id1", "id2", "id3", "id4", "id5", "id6"]') as array(varchar))

Output: Output:

_col0 _col0
[id1, id2, id3, id4, id5, id6] [id1, id2, id3, id4, id5, id6]

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

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