arrays - 查询数组元素的值(PostgreSQL)

标签 arrays postgresql postgresql-8.4

以下查询:

select unnest(Table2.L) as X, unnest(Table1.O) 
from Table1, Table2 
where Table1.code = Table2.code 
order by X ;

产生预期的结果。但是,我想排除(未嵌套的)匹配某些值的行。向查询添加条件,例如:

and unnest(Table2.L) != '-'

显然行不通。这可能吗?怎么办?

最佳答案

如果 unnest(Table2.L) != '-' 你的意思是

throw out all the unnested elements that are '-'

然后使用派生表并过滤掉不需要的未嵌套值:

select *
from (
    select unnest(Table2.L) as X, unnest(Table1.O) 
    from Table1 join Table2 on Table1.code = Table2.code
) dt
where X != '-'
order by X ;

如果你是说

ignore all rows from Table2 where L contains '-'

然后你可以使用 @> operator检查 L 是否包含某个元素:

select unnest(Table2.L) as X, unnest(Table1.O)
from Table1 join Table2 on Table1.code = Table2.code
where not Table1.L @> ARRAY['-']

或者你可以使用 ANY :

select unnest(Table2.L) as X, unnest(Table1.O)
from Table1 join Table2 on Table1.code = Table2.code
where not '-' = any(Table1.L)

忘记隐式连接的存在,总是使用显式连接条件,帮自己一个忙。

关于arrays - 查询数组元素的值(PostgreSQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11215128/

相关文章:

python - 为什么 Odoo 删除日期时间字段中的毫秒数?

sql - 如何在sql语句中复制一条记录

postgresql - 如何在 Npgsql 中使用 hstore 列类型?

sql - 如何在 PostgreSQL 中创建只读用户?

sql - postgresql 明显不工作

iOS - 重新排序 CoreData 中的项目

C++ 模拟 Pascal 中的记录数组

c++ - 为什么 C 数组比 std::array 快这么多?

sql - 尝试查找不同的数据时返回相同的结果

java - 我真的很难弄清楚如何让我的 GUI 窗口显示数组中的值