首页 > 解决方案 > Oracle -SQL- 使用 where 条件根据长列表选择值

问题描述

我有一长串(超过 2000 个)电话号码,我想选择电话号码在列表中的值。谁能告诉我如何使用 sql 有效地做到这一点?谢谢!

我知道的方法是:

select name, date from table where phone in ('8001234567','8011234567',....)

此方法列出 where 条件下的所有电话号码。有没有其他方法,比如我可以在某个文件中创建一个列表,然后从文件中选择?

谢谢你。

标签: sqloraclelistselectwhere-clause

解决方案


如果这只是一次性查询并且您不需要维护此列表,则可以使用 CTE 执行此操作:

with phone_list as (
    select '1234567890' as phone from dual union all
    select '2345678901' as phone from dual union all
    ...
    select '9999999999' as phone from dual
)
select name, date
  from table t
 inner join phone_list pl on pl.phone = t.phone

推荐阅读