从 Postgresql 表中选择不同的数组值

Jor*_*ash 3 postgresql distinct distinct-values

我有一个 PostgreSQL 数据库表,它看起来像这样

content
title          category              content
Hello World    ["Great"]             This is the content
Learn More     ["Learn", "Great"]    Learn about things
Run Code Online (Sandbox Code Playgroud)

我知道这不是存储此类数据的最佳方式,但此时无法更改。

我想在一个查询中获得一组独特的类别,如下所示:

SELECT DISTINCT category FROM content

最后得到一个这样的数组:

["Great", "Learn"]

我知道如果类别在单独的表中会很容易,但是如果它们像这样嵌套,你会怎么做?

category 是 JSONB 格式。

小智 11

在 Postgres 中,您可以使用数组函数,例如;

select distinct unnest(category) as nestCategory from content
Run Code Online (Sandbox Code Playgroud)

PostgreSQL 数组函数