Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create 1000 rows with the same values in columns?

Tags:

mysql

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.

Any suggestion?

like image 973
marian Avatar asked Jun 01 '11 18:06

marian


2 Answers

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);
like image 82
Nicola Cossu Avatar answered Sep 26 '22 03:09

Nicola Cossu


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.

$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);
}
like image 43
Ibrahim Azhar Armar Avatar answered Sep 25 '22 03:09

Ibrahim Azhar Armar