sql - 按范围选择表值
问题描述
在我的表中,我有主键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
我的问题是如何改进它?我可以将字符串转换为数字然后使用>89
or<90
吗?
解决方案
您还可以使用正则表达式:
select * from MyTable
where REGEXP_LIKE(id, '^[0-8][0-9]') and status='active'
or REGEXP_LIKE(id, '^9[0-9]') and status='inactive'
推荐阅读
- c# - ASP.NET 不加载 CSS 文件
- android - 是否可以使用 .bat 文件在 Jenkins 中运行 android GUI 模拟器?
- python - Pyautogui点击但它不会移动鼠标
- php - 如何从 Web 服务器中的终端返回结果
- node.js - Docker 映像无法在 AWS EC2 linux 上构建,但在 Windows 和 Mac 上构建时可以工作
- python - 如何删除 tkinter 中的小部件?
- c# - Unity 在函数调用后崩溃。是数组索引错误吗?
- python - ModuleNotFoundError:尝试绝对导入时没有名为“app”的模块
- ubuntu - 在使用 UniDic 2.3.0 构建 MeCab 0.996 用户字典时,如何确定左右上下文 ID 应该是什么?
- node.js - Node.js 头盔和 swagger-ui