[英]Postgres IN clause on a two column unique constraint?
我有一個表,該表具有跨兩列( code
和print
)的唯一約束,以及一組代碼和打印。 見下文:
// these two arrays can get very big
codes: ('A', 'B', 'C', 'D')
prints: (1, 2, 3, 4)
有沒有辦法在一個查詢中獲取代碼 A 並打印 1、代碼 B 和打印 2 等的行? 我最初的想法是使用SELECT * FROM table WHERE code IN (...)
查詢,但我不太確定如何正確地將print
“映射”到正確的code
。
如果有什么不合理的地方,請告訴我,我會盡力澄清。 謝謝!
你可以試試這個:
SELECT t.*
FROM table AS t
INNER JOIN unnest(array['A', 'B', 'C', 'D'], array[1, 2, 3, 4]) AS a(code, print)
ON t.code = a.code
AND t.print = a.print
arrays 中的每個元素根據它們在 arrays 中的順序在unnest()
function 中兩個兩個耦合。
select the_table.* from the_table join unnest ('{A,B,C,D}'::text[], '{1,2,3,4}'::int[]) as t(a, b) on (code, print) = (t.a, t.b);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.