views:

28

answers:

2

I need to insert 50 rows into a mysql table. They should all be exactly identical, besides the primary key.

Is there a query that can do this?

+2  A: 

Just use while loop:

declare var1 int
select var1 = 0
BEGIN 
 WHILE (var1 < 50) DO 
   insert...
   Select var1 = var1 + 1
 END WHILE;
END
sza
Awesome, thanks!
Cam
Hmm. Tried it out, but the insert statement isn't working (it spits out an error, pointing to the insert statement and tells me to check my syntax there). I'm using `INSERT INTO myTable (data,num) VALUES ('textdata',1234)`. Weird thing is, that works fine on its own as a single insert. Any ideas?
Cam
As well, how do i increment the var1 counter?
Cam
Is your primary key an auto increment?
galford13x
@galford13x: Yes.
Cam
A: 

Brutal but efficient:

INSERT INTO MyTable (data, num)
SELECT 'textdata' as data, 1234 as num
FROM
( SELECT 1
  UNION ALL
  SELECT 1
) as a,
( SELECT 1
  UNION ALL
  SELECT 1
  UNION ALL
  SELECT 1
  UNION ALL
  SELECT 1
  UNION ALL
  SELECT 1
) as b,
( SELECT 1
  UNION ALL
  SELECT 1
  UNION ALL
  SELECT 1
  UNION ALL
  SELECT 1
  UNION ALL
  SELECT 1
) as c
newtover