我正在嘗試這種邏輯,不確定在這種情況下缺少什么
select *
from table1 t1
join table2 t2 on t1.column1=t2.column1
where t1.column1 between 1 and 10 if t2.column2='value1'
and t1.column1 between 11 and 20 if t2.column2='value2'
uj5u.com熱心網友回復:
你可以在接近時使用案例
CREATE tABLE table2(column1 int, column2 varchar(10))
INSERT INTO table2 VALUEs(1,'value1'),(2,'value1'),(13,'value2'),(44,'value2')
CREATE tABLE table1(column1 int)
INSERT INTO table1 VALUES (1),(2),(13),(44)
select * from table1 t1 join table2 t2 on t1.column1=t2.column1 where CASE WHEN t1.column1 between 1 and 10 AND t2.column2 like 'value1' THEN TRUE WHEN t1.column1 between 11 and 20 AND t2.column2 like 'value2' THEN TRUE ELSE FALSE END
第 1 列 | 第 1 列 | 第 2 列
------: | ------: | :------
1 | 1 | 價值1
2 | 2 | 價值1
13 | 13 | 價值2
db<>在這里擺弄
uj5u.com熱心網友回復:
為了簡單起見,我假設您省略了關鍵細節,例如 JOIN 的 ON 和 LIKE 的通配符。
select *
from table1 t1
join table2 t2
where (t1.column1 between 1 and 10 and t2.column2 like 'value1')
or (t1.column1 between 11 and 20 and t2.column2 like 'value2')
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/492343.html
標籤:sql PostgreSQL
