我正在尝试在MySQL中生成一个连续数字的大表。我只想要2列; 主键和数字列,范围为0-X,其中X非常大。约。64,000行应该这样做。我试过这段代码没有成功:CREATE TABLE numbers ( number INT NOT NULL CONSTRAINT XPKnumbers PRIMARY KEY CLUSTERED (number) )INSERT INTO numbers (number) VALUES (0)DECLARE @i INTSET @i = 20WHILE 0 < @i BEGIN INSERT INTO numbers (number) SELECT number + (SELECT 1 + Max(number) FROM numbers) FROM numbers SET @i = @i - 1 ENDSELECT * FROM numbers我收到此错误:#1064 - 您的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在'CONSTRAINT XPKnumbers PRIMARY KEY CLUSTERED(数字)附近使用正确的语法。在第3行插入n'有人建议让这项工作吗?
3 回答
白板的微信
TA贡献1883条经验 获得超3个赞
您在列和约束声明之间缺少逗号:
CREATE TABLE numbers (
number INT NOT NULL,
CONSTRAINT XPKnumbers
PRIMARY KEY CLUSTERED (number)
)
慕桂英4014372
TA贡献1871条经验 获得超13个赞
从MySQL 8.0开始,您可以使用RECURSIVE CTE生成计数表:
SET @@cte_max_recursion_depth = 5000;
WITH RECURSIVE cte AS
(
SELECT 1 AS i
UNION ALL
SELECT i+1
FROM cte
WHERE i < 3000
)
SELECT *
FROM cte;
添加回答
举报
0/150
提交
取消