本文介紹了在 MySQL 中生成一系列數字的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何從 MySQL 查詢生成一系列連續數字(每行一個),以便將它們插入表中?
How do I generate a range of consecutive numbers (one per line) from a MySQL query so that I can insert them into a table?
例如:
nr
1
2
3
4
5
為此我只想使用 MySQL(而不是 PHP 或其他語言).
I would like to use only MySQL for this (not PHP or other languages).
推薦答案
如果您需要表中的記錄,并且想要避免并發問題,請按以下步驟操作.
If you need the records in a table and you want to avoid concurrency issues, here's how to do it.
首先你創建一個表來存儲你的記錄
First you create a table in which to store your records
CREATE TABLE `incr` (
`Id` int(11) NOT NULL auto_increment,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
第二次創建一個這樣的存儲過程:
Secondly create a stored procedure like this:
DELIMITER ;;
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO
INSERT incr VALUES (NULL);
SET v1 = v1 - 1;
END WHILE;
END;;
DELIMITER ;
最后調用 SP:
CALL dowhile();
SELECT * FROM incr;
結果
Id
1
2
3
4
5
這篇關于在 MySQL 中生成一系列數字的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!