在 PostgreSQL 中,我有一個如下表。
| id | mycolumn |
| -------- | -------------- |
| 1 | /1/2/8/ |
| 2 | /1/40/22/11/ |
| 3 | /1/15/35/ |
我正在嘗試獲取陣列中每一行的所有數字。到目前為止,我想出了以下內容:
SELECT array_agg(matchx)
FROM (
SELECT unnest(regexp_matches(mycolumn,
'[0-9] ',
'g')) matchx
FROM mytable
) x
我想要的是:
| array_agg |
| ------------ |
| {1,2,8} |
| {1,40,22,11} |
| {1,15,35} |
我得到什么:
| array_agg |
| ----------------------------- |
| {1,2,8,1,40,22,11,1,15,35} |
如何獲得每行的陣列?
uj5u.com熱心網友回復:
無需解嵌套,直接轉成陣列即可:
select id, string_to_array(trim(both '/' from mycolumn), '/')
from mytable;
是必要的trim(),否則陣列中會有空元素。
如果您在匯總時進行分組,則您的原始方法將起作用:
SELECT id, array_agg(matchx)
FROM (
SELECT id,
unnest(regexp_matches(mycolumn,'[0-9] ','g')) matchx
FROM mytable
) x
group by id;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/467421.html
標籤:数组 细绳 PostgreSQL
