在Oracle SQL中,我需要使用SELECT查询按多个值对过滤记录。有一个FIRST_ID和SECOND_ID,我希望仅按指定对过滤数据。
我尝试用第一种方法使用CONCAT,接下来我用OR运算符准备了很多对,但这两种方法都需要大量的手工工作。
select *
from table_data
where to_char(first_id||;||second_id) in ('123;354', '422;563', ... '353;536');
或
select *
from table_data
where (first_id = 123 and second_id = 354)
or (first_id = 422 and second_id = 563)
or (first_id = 353 and second_id = 536);
因此,您看到我不能使用两个IN运算符(一个用于first_id,第二个用于second_id),因为它将为所有crosing对提供结果,例如123-254、123-562和123-536等。任何想法如何快速简便地做到这一点?
答案 0 :(得分:2)
Oracle通过元组支持IN
:
select *
from table_data
where (first_id, second_id) in ( (123, 354), (422, 563), (353, 536));