如何在列中创建具有相同值的1000行?

时间:2022-08-27 18:16:45

I would like to create e.g. 1000 rows with the same values for each column in my table (the only difference would be the autoincrement first id column), however I do not know how to write this mysql statement.

我想创建例如我的表中每列的1000行具有相同的值(唯一的区别是autoincrement first id列),但是我不知道如何编写这个mysql语句。

Any suggestion?

2 个解决方案

#1


12  

create table mytest (
id int not null auto_increment primary key,
col1 varchar(10),
col2 varchar(10)
) engine = myisam;

delimiter //
create procedure populate (in num int)
begin
declare i int default 0;
while i < num do
insert into mytest (col1,col2) values ('col1_value','col2_value');
set i = i + 1;
end while;
end //
delimiter ;

call populate (1000);

#2


0  

i didn't know you could do that in MySQL but @nick rulez have better answer i guess , however this is how you can do it via PHP.

我不知道你可以在MySQL中做到这一点,但@nick rulez我猜有更好的答案,不过这是你可以通过PHP做到的。

$host = 'localhost';
$username = 'myusername';
$password = 'mypassword';
$connect = mysql_connect($host, $username, $password);

for($i=0; $i<=1000; $i++) {
   $query = 'INSERT INTO tablename(column1, column2, column3) VALUES (value1, value2, value3)';
   mysql_query($query);
}

#1


12  

create table mytest (
id int not null auto_increment primary key,
col1 varchar(10),
col2 varchar(10)
) engine = myisam;

delimiter //
create procedure populate (in num int)
begin
declare i int default 0;
while i < num do
insert into mytest (col1,col2) values ('col1_value','col2_value');
set i = i + 1;
end while;
end //
delimiter ;

call populate (1000);

#2


0  

i didn't know you could do that in MySQL but @nick rulez have better answer i guess , however this is how you can do it via PHP.

我不知道你可以在MySQL中做到这一点,但@nick rulez我猜有更好的答案,不过这是你可以通过PHP做到的。

$host = 'localhost';
$username = 'myusername';
$password = 'mypassword';
$connect = mysql_connect($host, $username, $password);

for($i=0; $i<=1000; $i++) {
   $query = 'INSERT INTO tablename(column1, column2, column3) VALUES (value1, value2, value3)';
   mysql_query($query);
}