簡體   English   中英

在Postgres中將逗號分隔的字符串轉換為整數數組

[英]Converting comma separated string to integer array in Postgres

我試圖將逗號分隔的字符串轉換為整數數組(integer [])以在Where子句中使用。

我試過cast, ::Int這個沒用。 感謝您的意見

Table A   |  Table B
ID        |  Set_id
2         |  14,16,17
1         |  15,19,20
3         |  21

我的查詢:

Select * 
from Table a, table b 
where a.id in b.set_id

如果要將其用於連接條件,則需要將字符串轉換為正確的整數數組。

Select * 
from Table a
  join table b on a.id = any(string_to_array(b.set_id, ',')::int[]);

但是,一個更好的解決辦法是正確正常化你的表(在整型數組或至少存儲這些標識,而不是一個varchar列)

Select * from Table_a a, table_b  b
where a.id = any(regexp_split_to_array(b.set_id,',')::int[]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM