oracle 如何在order by子句后选择starting from where id equals某个值?

twh00eeo  于 2023-06-22  发布在  Oracle
关注(0)|答案(2)|浏览(91)

我想选择从order by之后找到特定id的行开始的记录,但我不知道如何做到这一点。主要是因为我们对这些数据的批处理在一个特定的id处停止了,我需要从该id恢复,以将该数据添加到一个文件中(从该id所在的位置开始拉回数据)。
以下是我的查询:

Select * from TABLE_A a left join TABLE_B b
       on a.TABLE_B_ID = b.TABLE_B_ID
    WHERE b.CODE is not null and b.OTHER_FIELD is not null
    ORDER BY a.TABLE_A_ID
    //STARTING WITH a.TABLE_A_ID = 344456 <---I want to do something like this. How?
2vuwiymt

2vuwiymt1#

您可以将条件移动到WHERE子句:

SELECT * FROM TABLE_A a LEFT JOIN TABLE_B b
    ON a.TABLE_B_ID = b.TABLE_B_ID
WHERE b.CODE IS NOT NULL
AND b.OTHER_FIELD IS NOT NULL
AND a.TABLE_A_ID >= 344456
ORDER BY a.TABLE_A_ID
kupeojn6

kupeojn62#

ORDER BY之前使用>=进行过滤:

Select *
from   TABLE_A a
       INNER JOIN TABLE_B b
       on a.TABLE_B_ID = b.TABLE_B_ID
WHERE  b.CODE is not null
AND    b.OTHER_FIELD is not null
AND    a.TABLE_A_ID >= 344456
ORDER BY a.TABLE_A_ID

注意:如果你要求b.other_fieldb.code是非NULL,那么你将隐式地将连接从LEFT OUTER JOIN转换为INNER JOIN,因为永远不会出现OUTER JOIN出现并且这些列是非NULL的情况。
如果你想要一个OUTER JOIN,那么在连接条件中过滤:

Select *
from   TABLE_A a
       LEFT OUTER JOIN TABLE_B b
       on (   a.TABLE_B_ID = b.TABLE_B_ID
          AND b.CODE is not null
          AND b.OTHER_FIELD is not null
          )
WHERE  a.TABLE_A_ID >= 344456
ORDER BY a.TABLE_A_ID

相关问题