Postgres: filtering results using ARRAY_AGG and HAVING (instead of WHERE)

后端 未结 1 1265
一生所求
一生所求 2021-02-19 09:05

So my database contains a listing of items:

items:  item.id | item.title

And I have a second table that associates a number of tags with the it

1条回答
  •  遥遥无期
    2021-02-19 09:23

    Turns out that Postgres's ANY keyword is sided and can't be used symmetrically.

    Thus the working code is:

    SELECT items.id, items.title, ARRAY_AGG(tags.tag_id)
    FROM items
    INNER JOIN tags ON (tag.tag_id=items.id)
    GROUP BY items.id
    HAVING 27 = ANY(ARRAY_AGG(tags.tag_id))
    

    0 讨论(0)
提交回复
热议问题