首页 > 解决方案 > Oracle 在 SQL 查询中使用变量

问题描述

我有这样的事情:

With a as (select id from table_a)
select * from table_b where table_b.id > (select min(id) from a)

table_a 是一个包含数百万条记录的巨大表,我不想每次要使用 min(i) 时都通过所有记录来查找 min(i)。我有什么方法可以将 min(id) 存储在变量中并在查询中使用它?像这样的东西:

With a as (select id from table_a),
b as ((select min(id) into min_id from a))
select * from table_b where table_b.id > min_id

标签: sqldatabaseoracle

解决方案


你的查询应该做你想做的事。但是,如果您真的想确定,请将子查询移至from子句:

select b.*
from table_b b join
     (select min(id) as min_id from a) a
     on b.id > a.min_id;

推荐阅读