我的查询:
select
s.id,
s.title,
string_agg(t1.title, ',') as a,
string_agg(t2.title, ',') as b,
string_agg(t3.title, ',') as c,
string_agg(t4.title, ',') as d
from show as s
left join tag as t1 on t1.show_id = s.id and t1.type='a'
left join tag as t2 on t2.show_id = s.id and t2.type='b'
left join tag as t3 on t3.show_id = s.id and t3.type='c'
left join tag as t4 on t4.show_id = s.id and t4.type='d'
group by
s.id,
s.title
having
t1.title = 'Something';
引发错误:
错误:列" t1.title"必须出现在GROUP BY子句中或者是 用于聚合函数
我不明白的是string_agg是聚合函数,根据这个手册页:
http://www.postgresql.org/docs/9.4/static/functions-aggregate.html
我的数据:
show:
id: 1
title: 'test'
tag:
id: 1
show_id: 1
type: 'a'
title: 'title a'
tag:
id: 2
show_id: 1
type: 'b'
title: 'title a'
tag:
id: 3
show_id: 1
type: 'c'
title: 'title c'
tag:
id: 4
show_id: 1
type: 'd'
title: 'title d'
答案 0 :(得分:2)
having命令正在尝试搜索聚合字段。
通过s.title搜索。或者由t1.title分组。
having
s.title = 'Something';
或者可以更改必须在concat字段上搜索。
具有string_agg(t1.title,',')=' Something';