这是我的方案,
我有桌子
ID(PK) TYPE(PK)
01 3
02 3
03 3
04 3
现在我必须在类型3中插入ID高达1000 ... 是否有任何选择自动执行???
答案 0 :(得分:5)
使用存储过程
DELIMITER $$
CREATE PROCEDURE insertMany(Pstart_id INT, Pend_id INT, Ptype INT)
BEGIN
DECLARE i INT;
IF (pstart_id > pend_id) THEN BEGIN
SET i = PEnd_id;
SET Pend_id = Pstart_id;
SET Pstart_id = Pend_id;
END; END IF;
SET i = Pstart_id;
WHILE I <= Pend_id DO BEGIN
INSERT INTO table1 (id, `type`) VALUES (i, Ptype);
SET i = i + 1;
END; END WHILE;
END $$
DELIMITER ;
SELECT @first_id:= max(id)+1, @last_id:= max(id)+2001 FROM table1;
CALL InsertMany(@first_id, @last_id, 3);
这比使用php循环更快,因为你没有通过网络发送1000个插入查询。
或者您可以查看此问题:How to select a single row a 100 million x
答案 1 :(得分:1)
如果ID
是数字:
INSERT INTO TableX
( ID, TYPE )
VALUES
(1, 3) ,
(2, 3) ,
(3, 3) ,
(4, 3) ,
(5, 3) ,
(6, 3) ;
INSERT INTO TableX
( ID, TYPE )
SELECT
ID, 3
FROM
( SELECT
7+ d0.ID - 1 + (d1.ID * 6) - 6 + (d2.ID * 36) - 36 + (d3.ID * 216) - 216
AS ID
FROM
TableX AS d0
CROSS JOIN
TableX AS d1
CROSS JOIN
TableX AS d2
CROSS JOIN
TableX AS d3
WHERE d0.TYPE = 3 AND d0.ID <= 6
AND d1.TYPE = 3 AND d1.ID <= 6
AND d2.TYPE = 3 AND d2.ID <= 6
AND d3.TYPE = 3 AND d3.ID <= 6
) AS tmp
WHERE ID <= 1000 ;
答案 2 :(得分:0)
不确定MySQL查询但是因为你熟悉PHP,为什么不写一个非常简单的PHP脚本循环1000次并插入..?
答案 3 :(得分:0)
也许您可以尝试http://www.generatedata.com/#generator自动插入。