鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 数据库 > SQLite > >

生成1到300个数字的方法

来源:互联网 作者:佚名 时间:2017-09-09 08:24
生成1到300个数字的方法 方法一 cross join SELECT aa. [ num ] + bb. [ num ] + cc. [ num ] FROM ( SELECT 0 num UNION ALL SELECT 1 num UNION ALL SELECT 2 num UNION ALL SELECT 3 num UNION ALL SELECT 4 num UNION ALL SELECT 5 num UNION ALL SELECT

生成1到300个数字的方法

 

方法一

cross join

SELECT aa.[num]+bb.[num]+cc.[num] FROM 
(SELECT 0 num UNION ALL
SELECT 1 num UNION ALL
SELECT 2 num UNION ALL
SELECT 3 num UNION ALL
SELECT 4 num UNION ALL
SELECT 5 num UNION ALL
SELECT 6 num UNION ALL
SELECT 7 num UNION ALL
SELECT 8 num UNION ALL
SELECT 9 num ) aa
CROSS JOIN
(SELECT 0 num UNION ALL
SELECT 10 num UNION ALL
SELECT 20 num UNION ALL
SELECT 30 num UNION ALL
SELECT 40 num UNION ALL
SELECT 50 num UNION ALL
SELECT 60 num UNION ALL
SELECT 70 num UNION ALL
SELECT 80 num UNION ALL
SELECT 90 num ) bb
CROSS JOIN
(SELECT 0 num UNION ALL
SELECT 100num UNION ALL
SELECT 200 num  ) cc
ORDER BY 1

 

 

 

方法二

while循环

DECLARE @i INT
DECLARE @tb TABLE(a INT)
SET @i=1
    INSERT INTO @tb
            ( [a] )
    VALUES  (  @i  -- a - int
              )
WHILE (@i<300)
BEGIN
    SET @i=@i+1
    INSERT INTO @tb
            ( [a] )
    VALUES  (  @i  -- a - int
              )
END
SELECT * FROM @tb

 

方法三

CTE递归

;with cte_temp
as
(
    select 0 as id
    union all
    select id+1 from cte_temp where id<301
)
select id from cte_temp option (maxrecursion 301);

 

网友评论
<