PostgreSQL中如何在array_to_string()拼接时过滤特定值
一行实现数组过滤并拼接的PostgreSQL方案
过滤单个特定值
利用array_remove()先移除目标元素,再用array_to_string()拼接,代码简洁直接:
SELECT array_to_string(array_remove(hashtags, '要过滤的标签'), ';') AS filtered_hashtags FROM 你的表名;
示例:若原数组为{'tree', 'house'},过滤'house'后会得到结果'tree'。
过滤多个特定值
要排除多个值时,用子查询生成过滤后的数组,同样一行完成:
SELECT array_to_string(array(SELECT unnest(hashtags) WHERE unnest NOT IN ('标签1', '标签2')), ';') AS filtered_hashtags FROM 你的表名;
示例:若原数组为{'tree', 'house', 'car'},过滤'tree'和'car'后会得到结果'house'。
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

