sql:将字符串转换为数组

7uzetpgm  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(783)

我有下表-示例表:

使用dbquery:

let query = `
        SELECT *
        FROM "ExampleTable "`;
return this.dbQuery(query, []);

我得到的数组如下所示:

result = ["First":111,"Second":222,Third:"Yes/No/Unknown"]

问题:如何将字符串“yes/no/unknown”转换为一个附加数组,以便实现以下功能?

result = ["First":111,"Second":222,Third:["Yes","No","Unknown"]]
c3frrgcw

c3frrgcw1#

如果“first”和“second”是这个表的主键,那么下面的查询就起作用了。但是通过js拆分数组更容易

select first, second, JSON_ARRAYAGG(val) as third
from (
    SELECT first, second, substring_index(third,'/',1) as val
    FROM ExampleTable

    union all

    SELECT first, second, substring_index(substring_index(third,'/',2),'/',-1) as val
    FROM ExampleTable 

    union all

    SELECT first, second, substring_index(third,'/',-1) as val
    FROM ExampleTable 
) un
group by first, second

相关问题