sql - 基于sql中输入范围的数字范围拆分
问题描述
我有表(table1),数据为
SlNo NumberStart NumberEnd Volume
1 101 110 10
2 301 301 1
我的 jquery 输入是 Number of volume is requested as
Volume: 3
NumberStart NumberEnd
301 301
103 103
当我过滤 table1 数据中的输入时,选择语句结果应拆分如下
NumberStart NumberEnd isSplitted Volume
301 301 Yes 1
101 102 Yes 2
103 103 Yes 1
104 110 No 7
所以我要求的第 3 卷被拆分了。
您能否请任何人分享上述的 SELECT 语句?
解决方案
您试图实现的确切逻辑有点模糊。您似乎正在table1
根据table2
. 对于匹配的部分,is_splitted = 1
.
以下返回您指定的结果:
select t1.NumberStart, t2.NumberEnd, 1 as is_splitted, t2.NumberEnd - t1.NumberStart as volume
from table1 t1 join
table2 t2
on t1.NumberStart = t2.NumberStart
union all
select t2.NumberStart + 1, t1.NumberEnd, 0 as is_splitted, t1.NumberEnd - t2.NumberEnd as volume
from table1 t1 join
table2 t2
on t1.NumberStart = t2.NumberStart
where t2.NumberEnd <> t1.NumberEnd;
这是一个 db<>fiddle。
推荐阅读
- database - 关于sqllite的一个简单问题,在学习过程中卡住了
- python - NameError:名称“公会”未定义。问题是什么?
- apache-kafka - 如何使生产者和消费者需要 Apache Kafka 客户端 ID?
- c# - OrientDb 查询执行时间
- javascript - 我无法让我的代码检查一个数字是否已经在数组中 | JavaScript
- python - 我的 python 代码正在更改列表中的变量,但我看不到它是如何/为什么被更改的
- django - 如何强制 django.allauth 要求电子邮件
- java - 部署目标期间 maven-resources-plugin 中的 IllegalArgumentException
- python - KivyMD:在 python 代码中添加列表小部件,“on_release”问题 - 属性
- c - 尝试创建一个 C 程序,逐字查找字符串的反转