我想使用 for 循环将 100 到 9999 之间的数字添加到表中。我尝试过以下查询:
DELIMITER //
BEGIN
FOR phone_number IN 100..9999 LOOP
INSERT INTO phones (`phoneid`,`phone`,`active`) VALUES (NULL, phone_number, "1");
END LOOP;
END
//
我收到以下错误
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near'FOR phone_number IN 100..9999 LOOP INSERT INTO phones (`ph' at line 2
出了什么问题?
最佳答案
我不认为mysql中存在“for循环”。你可以使用这个:
DELIMITER //
DROP PROCEDURE IF EXISTS ins//
CREATE PROCEDURE ins()
BEGIN
DECLARE cnt INT;
SET cnt=100;
WHILE cnt<10000 DO
INSERT INTO phones (`phoneid`,`phone`,`active`) VALUES (NULL, cnt, "1");
SET cnt=cnt+1;
END WHILE;
END//
DELIMITER ;
CALL ins();
关于mysql - 如何在 MySQL 上仅使用 SQL 将数字添加到表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25160769/