Postgresql - Opposite of string_agg

吃可爱长大的小学妹 提交于 2019-12-12 12:07:17

问题


I'm looking for a postgresql function that will do the opposite of string_agg.

I have a movies table where the tags column contains values such as

Action|Adventure|Drama|Horror|Sci-Fi
Action|Horror|Sci-Fi

I would like to get a distinct list of tags from this column, for example

Action
Adventure
Drama
Horror
Sci-Fi

回答1:


You can use unnest() and string_to_array():

 select unnest(string_to_array(t.col, ','))
 from t



回答2:


You need string_to_array() combined with unnest()

select t.tag
from movies, unnest(string_to_array(tags,'|')) as t(tag)


来源:https://stackoverflow.com/questions/42736131/postgresql-opposite-of-string-agg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!