使用Postgres数组选择数据

时间:2013-02-25 11:21:02

标签: arrays postgresql

我有一个包含user_ids列的表,这是一个Postgres数组。

我需要从另一个表中选择所有messages,其中列user_id是给定数组中的一个ID。

在Psuedo-sql中:

select users.*
from users
where id IN a_postgres_array

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您可以使用ANY运算符。来自您的样本:

select users.*
from users
where id =ANY(a_postgres_array)

使用两个表时,它可能是JOIN,类似于:

SELECT users.*
FROM users INNER JOIN table_with_array ON users.id =ANY(table_with_array.a_postgres_array)

答案 1 :(得分:0)

select users.*
from users
where id IN (
    select unnest(a_postgres_array)
    from t
    where columnX = some_value
    )