![](/img/trans.png)
[英]How to use a WHERE statement in Postgres with an array of OR combinations?
[英]How to make use of a postgres Array in where query?
postgreSQL 中的一個表有一列數據類型為 text[]
TableA:
id uuid
tableb_ids text[]
TableB:
id uuid
name text
現在我需要編寫如下查詢:
select * from tableB where id in (select tableb_ids from tableA where id ="xxxx-xxxx-xxxx-xxxx")
我無法更改架構/表定義。 即)我不能為tableA中的tableB的每個條目保留很多條目。 TableA 是一個復雜的表。
要在一個表中查找其記錄 id 包含在其他表的數組中的記錄,您可以加入這些表:
SELECT b.*
FROM tableb b
INNER JOIN tablea a
ON b.id::TEXT = ANY(a.tableb_ids)
AND a.id = 'xxxx-xxxx-xxxx-xxxx'
你可以這樣做的另一種方法:
SELECT b.*
FROM tableb b
WHERE id IN (
SELECT UNNEST(a.tableb_ids)
FROM tablea a
WHERE a.id = 'xxxx-xxxx-xxxx-xxxx'
) x
-- not sure if the aliases are needed in the subquery
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.