2013-04-05 58 views
3

我使用array_agg()函数将多行转换为数组, 我需要将该数组赋给select语句的条件。Postgresql从数组中选择值

我的查询,

SELECT * FROM table WHERE id = 
    ALL(SELECT array_agg(id) FROM table WHERE some_condition) 

,但它给错误,我怎么能到这儿来呢..

+0

和错误? – h22 2013-04-05 06:23:27

+0

你得到的错误是什么? – 2013-04-05 06:23:40

+0

错误:运算符不存在:bigint = bigint []提示:没有运算符匹配给定的名称和参数类型。您可能需要添加显式类型转换。 – 2013-04-05 06:26:00

回答

3

错误已被铸造型数组清除,用我这样的查询

SELECT * FROM table WHERE id = 
    ALL((SELECT array_agg(id) FROM table WHERE some_condition)::bigint[]) 

reference link

1

好像你是过于复杂的事情。据我所知,您的查询应该是等同于简单:

SELECT * FROM table WHERE some_condition 

或者,如果你是从2个不同的表中选择,使用加入:

SELECT table1.* 
FROM table1 
JOIN table2 ON table1.id = table2.id 
WHERE some_condition 

这不仅是简单的,它比数组摆弄还快。

+0

我需要记录的数量, – 2013-04-05 06:34:01

+0

这是没有提到你的问题。 'SELECT count(*)...'有什么问题? – mvp 2013-04-05 06:35:41