是否有可能选择增量数字,例如:
num
1000
1001
1002
1003
1004
...
1999
我试过了:
set @i=999;
select @i:=@i+1 as num
limit 1000
还有:
set @i=999;
select @i:=@i+1 as num
where @i <1999;
答案 0 :(得分:0)
我假设(可能是错误地)你试图根据自动递增的主键选择行,看到这是MySQL吗?
“BETWEEN”关键字是否可以更好地使用,而不是使用变量?
例如,如果您想从表'myTable'中选择行,并且'num'是主键:
SELECT num FROM myTable
WHERE num BETWEEN 999 AND 1999
答案 1 :(得分:0)
你必须在1000行表上操作才能得到你想要的东西:
SELECT @i := @i+1 AS num
FROM (
SELECT 1 AS x UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 AS x UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
) AS t
CROSS JOIN (
SELECT 1 AS x UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 AS x UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
) AS s
CROSS JOIN (
SELECT 1 AS x UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 AS x UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
) AS u
CROSS JOIN (SELECT @i := 999) AS var
在10 x 10 x 10表组合上使用CROSS JOIN
,我们得到一个1000行内联表。以此为基础,我们可以计算@i
值所需的序列。
答案 2 :(得分:0)
这是一个简单的查询,以设置增量值打印数字1000 - 1999。
DECLARE @firstNumber INT
DECLARE @lastNumber INT
DECLARE @incrementBy INT
SET @firstNumber = 1000
SET @lastNumber = 1999
SET @incrementBy = 1
CREATE TABLE #TEMP ( number INT)
WHILE(@firstNumber <= @lastNumber)
BEGIN
INSERT INTO #TEMP VALUES (@firstNumber)
SET @firstNumber = @firstNumber + @incrementBy
END
SELECT * FROM #TEMP
DROP TABLE #TEMP