Select 来自 Postgresql 的不同数组值 Table
Select Distinct Array Values from Postgresql Table
我有一个 PostgreSQL 数据库 table 看起来像这样
content
title category content
Hello World ["Great"] This is the content
Learn More ["Learn", "Great"] Learn about things
我知道这不是存储数据的最佳方式,但目前无法更改。
我想在一次查询中获得一组独特的类别,像这样:
SELECT DISTINCT category FROM content
最后得到一个这样的数组:
["Great", "Learn"]
我知道如果类别在单独的 table 中会很容易,但如果它们像这样嵌套,你会怎么做?
category
是 JSONB 格式。
在 Postgres 中,您可以使用数组函数,例如;
select distinct unnest(category) as nestCategory from content
我有一个 PostgreSQL 数据库 table 看起来像这样
content
title category content
Hello World ["Great"] This is the content
Learn More ["Learn", "Great"] Learn about things
我知道这不是存储数据的最佳方式,但目前无法更改。
我想在一次查询中获得一组独特的类别,像这样:
SELECT DISTINCT category FROM content
最后得到一个这样的数组:
["Great", "Learn"]
我知道如果类别在单独的 table 中会很容易,但如果它们像这样嵌套,你会怎么做?
category
是 JSONB 格式。
在 Postgres 中,您可以使用数组函数,例如;
select distinct unnest(category) as nestCategory from content