能否用SELECT语句生成等差数列结果集
给定 范围X,Y 步长S 求一条SELECT语句生成等差数列结果集
如 X=5.5 Y=8.0 S=0.5
要求输出结果如下:
begin end
5.5 6.0
6.0 6.5
6.5 7.0
7.0 7.5
7.5 8.0
------解决方案--------------------
仅用SQL不能。
如果一定要在数据库中实现,则你可以先建一张表 tseq(id int primary key); 其中存储足够多的序列 1,2,3,4,5.....N;
然后你可以
select @X+(id-1)*@S as begin, @X+(id)*@S as end from tseq where @X+(id-1)*S<=@Y order by id
------解决方案--------------------
SET @s=0.5;
SET @X=5.5;
SET @Y=8;
SELECT (@Y-@X)/@s;
SELECT *,@X+(id-1)*0.5 AS beg ,@X+(id)*@S AS END
FROM lsb1 WHERE id<=(@Y-@X)/@s
LSB1:自增字段ID,1-10000