首页 > 解决方案 > 按范围选择表值

问题描述

在我的表中,我有主键id。它是一个两字节的 varchar 类型。如果id是从 00 到 89 则返回一组数据,否则返回不同的数据。我在存储过程中的查询是

BEGIN
select * from MyTable where (id like '0%' or
                             id like '1%' or
                             id like '2%' or
                             id like '3%' or
                             id like '4%' or
                             id like '5%' or
                             id like '6%' or
                             id like '7%' or
                             id like '8%') 
                             and status = 'active'
union all
select * from MyTable where id like '9%' and status='inactive'
END

我的问题是如何改进它?我可以将字符串转换为数字然后使用>89or<90吗?

标签: sqloracle

解决方案


您还可以使用正则表达式:

select * from MyTable
where REGEXP_LIKE(id, '^[0-8][0-9]') and status='active'
or REGEXP_LIKE(id, '^9[0-9]') and status='inactive'

推荐阅读