PSQL unnest 函数未按预期工作
Posted
技术标签:
【中文标题】PSQL unnest 函数未按预期工作【英文标题】:PSQL unnest function not working as expected 【发布时间】:2017-06-08 17:18:37 【问题描述】:我尝试了this post 的解决方案,但仍然出现错误。
查询:
SELECT unnest(team)
FROM table_of_teams
WHERE team LIKE '%akg%';
错误:
ERROR: operator does not exist: character varying[] ~~ unknown
LINE 5: WHERE team LIKE '%akg%'
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
表结构:
Table "public.table_of_teams"
Column | Type | Modifiers
--------------------+-----------------------------+-------------------------------------------------
teamid | integer | not null default nextval('team_seq'::regclass)
index | integer |
name | character varying |
grouping | character varying |
hour_of_day | integer[] |
day_of_week | integer[] |
team | character varying[] |
【问题讨论】:
可以分享table_of_teams
的结构吗?
@Mureinik 共享。
【参考方案1】:
如果我理解正确并且您想提取满足给定条件的团队,您可以将 unnest
调用放在子查询中并将条件应用到周围查询中:
SELECT single_team
FROM (SELECT unnest(team) single_team
FROM table_of_teams) t
WHERE single_team LIKE '%akg%';
【讨论】:
做到了,谢谢。没有意识到需要嵌套查询。以上是关于PSQL unnest 函数未按预期工作的主要内容,如果未能解决你的问题,请参考以下文章
SQL Server STRING_AGG 函数排序未按预期工作