[英]How can I find array value occurrences and missing ones in a single query?
考慮一個數字字符串數組,例如 arr = ['202', '303', '1', ...] 和下表:
MyTable
code VARCHAR(255)
可以在單個查詢中找到:
有什么方法可以實現嗎?
我正在使用 PostgreSQL。
您可以取消嵌套數組並使用join
或類似的東西
對於第一個條件:
select el
from unnest(ar) el
where not exists (select 1 from t where t.code = el);
類似的邏輯可用於第二個,但您可能需要select distinct
:
select t.code
from t
where not exists (select 1 from unnest(ar) el where t.code = el);
如果您想在單個查詢中同時使用兩者,則可以使用union all
或full join
:
select el, t.code
from unnest(ar) el full join
t
on t.code = el
where t.code is null or el is null;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.