WITH temp AS (select * from t1 where c1 = 'string1')
select 'string1' as col1, t2.col2, temp.col3 from t2 inner join temp on t2.c2 = temp.c2 where t2.some_col like 'string1%'
union
WITH temp AS (select * from t1 where c1 = 'string2')
select 'string2' as col1, t2.col2, temp.col3 from t2 inner join temp on t2.c2 = temp.c2 where t2.some_col like 'string2%'
...
上面只是我嘗試運行的PostgreSQL查詢的一個示例。它是兩個完全相似的查詢的聯合。他們僅使用不同的值來匹配string1
和string2
。PostgreSQL:傳遞來自數組的值並執行聯合
我有大約20個這樣的查詢,我想做一個聯合。他們只能通過我想使用作爲string1
比較這樣的變量不同,怎樣才能用值['string1', 'string2', 'string3', .., 'string20']
這樣的陣列,從這個陣列和工會它們運行在每個變量的查詢?