以下查询:
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
whereL
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/