Mysql同行拷贝 - 同一表120次

时间:2015-09-02 07:31:19

标签: php mysql

简单的mysql表:

CREATE TABLE  `users` (
 `id` INT NOT NULL AUTO_INCREMENT ,
 `name` TEXT NOT NULL ,
 `surname` text NOT NULL ,
 `city` text NOT NULL ,
PRIMARY KEY (  `id` )
) ENGINE = MYISAM

插入如:

INSERT INTO `users` (`id`, `name`, `surname`, `city`) VALUES (1,`rock`,`fixed`,`london`);

如何使用不同的主ID进行100次相同的表格详细信息?

5 个答案:

答案 0 :(得分:3)

使用php你可以这样做:

for($i=0; $i< 119; $i++)
{
     //execute your query here, do not include your id column
     INSERT INTO `users` (`name`, `surname`, `city`)   
     VALUES   (`rock`,`fixed`,`london`);
 }  


您也可以使用mysql存储过程来执行此操作,如:

//Loop stored procedure

DELIMITER $$
DROP PROCEDURE IF EXISTS test$$
CREATE PROCEDURE test()
BEGIN
 DECLARE count INT DEFAULT 0;
 WHILE count < 119 DO
      INSERT INTO `users` (`name`, `surname`, `city`)   
      VALUES   (`rock`,`fixed`,`london`);
   SET count = count + 1;
 END WHILE;
END$$
DELIMITER ;
  

执行存储过程

call test();

基于评论的编辑:
您的代码无效,因为您在循环“外部”执行查询,您的代码应如下所示:

<?php 

*/here mysql connection 
for($x=0; $x<100; $x++) 
{ 
   $sql =INSERT INTO users (name, surname, city) 
          VALUES (rock,fixed,london); 
   $status = $conn->query($sql);
} 

if ($status) 
{  echo "New record created successfully"; }
else { echo "Error: " . $sql . "<br>" . $conn->error; } 
$conn->close(); 
?>

答案 1 :(得分:0)

尝试以下代码

for ($i=0; $i<= 119; $i++){
      //exclude id and do auto increament and primary key
       $qry = "INSERT INTO `users` (`name`, `surname`, `city`) VALUES (`rock`,`fixed`,`london`)";
}

答案 2 :(得分:0)

将其作为单个没有循环的SQL语句执行: -

INSERT INTO `users` (`id`, `name`, `surname`, `city`) VALUES (1,`rock`,`fixed`,`london`);
SELECT NULL, `rock`, `fixed`, `london`
FROM
(
    SELECT 0 AS aCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) units
CROSS JOIN
(
    SELECT 0 AS aCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11
) tens

2个子查询只返回数字范围。当交叉连接时,10合1和12在另一个中,它们将总共返回120行。那120行只是你的固定值,但id为NULL,自动增量将整理成唯一值。

请注意,您可以像这样做更大的范围。例如,如果您想插入653行(只是一个相当随机的数字选择): -

SELECT NULL, `rock`, `fixed`, `london`
FROM
(
    SELECT 0 AS aCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) units
CROSS JOIN
(
    SELECT 0 AS aCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 
) tens
CROSS JOIN
(
    SELECT 0 AS aCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 
) hundreds
WHERE (units.aCnt + 10 * tens.aCnt + 100 * hundreds.aCnt) < 653

或者将id字段指定为计算值: -

SELECT 1 + units.aCnt + 10 * tens.aCnt + 100 * hundreds.aCnt, `rock`, `fixed`, `london`
FROM
(
    SELECT 0 AS aCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) units
CROSS JOIN
(
    SELECT 0 AS aCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 
) tens
CROSS JOIN
(
    SELECT 0 AS aCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 
) hundreds
WHERE (units.aCnt + 10 * tens.aCnt + 100 * hundreds.aCnt) < 653

答案 3 :(得分:-1)

由于id具有属性AUTO_INCREMENT,因此每次输入数据时都应生成唯一ID。

所以你的插入字符串可以是这样的:

INSERT INTO `users` (`name`, `surname`, `city`) VALUES (`rock`, `fixed`, `london`);

id字段每次都会自动递增,为您提供唯一ID

答案 4 :(得分:-2)

只需使用此查询:

INSERT INTO users ( name, surname, city) VALUES ('rock','fixed','london');