以select 的结果作为where的必须条件

windlysnowly 2007-12-04 09:12:50
问题: 以select 的结果作为where的必须条件
比如: dent 表 (no,roes,start,end)
如果no在start和end 之间 ,我就给no加上roes。这些都是mumber
举例: no roes start end
1 2 1 10

执行的结果要求是这样的: no roes start end
3(2+1) 2 1 10
5 2 1 10
7 2 1 10
9 2 1 10
不用过程写,我想了半天。
...全文
442 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
littleboys 2007-12-06
  • 打赏
  • 举报
回复
如果不需要第一条记录,可以这样

with temp(no,roes,start,end) as
(select no+roes,roes,start,end from t_node
union all
select no+roes as no,roes,start,end
from temp
where no+roes<end
)
select * from temp;
littleboys 2007-12-06
  • 打赏
  • 举报
回复
with temp(no,roes,start,end) as
(select no,roes,start,end from t_node
union all
select no+roes as no,roes,start,end
from temp
where no+roes<end
)
select * from temp;
windlysnowly 2007-12-04
  • 打赏
  • 举报
回复
谢谢楼上的解答
如果我的数据库中只有一条记录
no roes start end
1 2 1 10

利用循环判断怎么才能得出下面的完全一样的结果
no roes start end
3 2 1 10
5 2 1 10
7 2 1 10
9 2 1 10

我具体解释一下: 库中只有一条记录 当no=1时,no在start和end之间 此时no变成 no+rose=3
no=3 同样也是在start和end之间,继续运算no+rose=5 ........
运行到9(在start和end之间)
继续no+rose=11
不符合条件退出
happyflog 2007-12-04
  • 打赏
  • 举报
回复
select (case when no>=start and no<=end then no+roes else no end) as no ,roes,start,end from dent
yangxiao_jiang 2007-12-04
  • 打赏
  • 举报
回复
select roes,case when (no > start and no <end) then no+roes else no END AS no from dent

5,889

社区成员

发帖
与我相关
我的任务
社区描述
IBM DB2 是美国IBM公司开发的一套关系型数据库管理系统,它主要的运行环境为UNIX(包括IBM自家的AIX)、Linux、IBM i(旧称OS/400)、z/OS,以及Windows服务器版本
社区管理员
  • DB2
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧