根据一个 sql 查询返回的结果,我必须决定调用哪个 SQL 查询。可能吗?
下面是一个演示:
select start, end, max from table.
If max < 10
select ob1, ob2, start, end from t1
if max >=10 and < 50
select ob1, ob2, start, end from t2
if max >= 50
select ob1, ob2, start, end from t2
最佳答案
您可以使用类似条件联合的东西:
select ob1, ob2, q2."start", q2."end"
from (
select "start", "end", "max"
from the_table
) q1,
lateral (
select ob1, ob2, "start", "end"
from t1
where "max" < 10
union
select ob1, ob2, "start", "end"
from t2
where "max" >=10 and "max" < 50
union
select ob1, ob2, "start", "end"
from t3
where "max" >= 50
) q2
了解 LATERAL Subqueries在文档中。
在 Postgres 9.2 中,可以使用 with
查询:
with m as (
select "max"
from the_table)
select ob1, ob2, q."start", q."end"
from (
select ob1, ob2, "start", "end"
from t1, m
where "max" < 10
union
select ob1, ob2, "start", "end"
from t2, m
where "max" >=10 and "max" < 50
union
select ob1, ob2, "start", "end"
from t3, m
where "max" >= 50
) q
关于postgresql - 根据另一个查询返回的条件在 postgres 中触发不同的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33146305/