I need to insert 50 rows into a mysql table. They should all be exactly identical, besides 开发者_StackOverflow社区the primary key.
Is there a query that can do this?
Just use while loop:
declare var1 int
select var1 = 0
BEGIN
WHILE (var1 < 50) DO
insert...
Select var1 = var1 + 1
END WHILE;
END
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
If you have a random existing table that has more rows than you want to insert, you could do this:
insert into table1 (row1,row2) select identical value1, identical value2 from table2 limit 50
e.g.:
insert into table1 (myint, mystr, mystr2) select 123, "hello", "world" from table2 limit 50
- Note that you are not actually inserting anything from table2
精彩评论