MySQL select 1000个增量数字
MySQL select 1000 increment numbers
是否有可能 select 递增数字,例如:
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;
我假设(可能是错误的)您正在尝试 select 基于自动递增主键的行,看到这是 MySQL?
使用 "BETWEEN" 关键字比使用变量更好吗?
例如,如果您想要 select 来自 table 'myTable' 的行,并且 'num' 是主键:
SELECT num FROM myTable
WHERE num BETWEEN 999 AND 1999
您必须对 1000 行进行操作 table 才能得到您想要的结果:
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 table 组合上使用 CROSS JOIN
,我们得到 1000 行内联 table。以此为基础,我们可以计算出所需的 @i
值序列。
这里是一个简单的查询,用于打印数字 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
是否有可能 select 递增数字,例如:
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;
我假设(可能是错误的)您正在尝试 select 基于自动递增主键的行,看到这是 MySQL?
使用 "BETWEEN" 关键字比使用变量更好吗?
例如,如果您想要 select 来自 table 'myTable' 的行,并且 'num' 是主键:
SELECT num FROM myTable
WHERE num BETWEEN 999 AND 1999
您必须对 1000 行进行操作 table 才能得到您想要的结果:
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 table 组合上使用 CROSS JOIN
,我们得到 1000 行内联 table。以此为基础,我们可以计算出所需的 @i
值序列。
这里是一个简单的查询,用于打印数字 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