0

we know that we can insert multiple records at a time using this query:

INSERT INTO `TABLE1` (`First`,`Last`) VALUES ('name1','surname1'),
('name2','surname2'),
('name3','surname3'),
('name4','surname4');

but what if we want to add 1000 similar records as above (name*,surname*) do we have to write down all the records or we can use something like wildcard? or is there any other solution using mysql?

2 Answers 2

1

Use this script:

set @a=0;
INSERT INTO `TABLE1` (`First`,`Last`) SELECT
CONCAT('Name',(@a:=@a+1)),CONCAT('Surname',@a)
FROM `information_schema`.`SESSION_VARIABLES` LIMIT 1000;

, where information_schema.SESSION_VARIABLES is any big table with at least 1000 rows.

1
  • This should have been asked on stackoverflow.com Jun 2, 2014 at 2:10
0
CREATE VIEW binary_view AS SELECT 0 n UNION SELECT 1;

SELECT CONCAT('name',s),CONCAT('surname',s) FROM (
SELECT
    b0.n * POW(2,0) +
    b1.n * POW(2,1) +
    b2.n * POW(2,2) +
    b3.n * POW(2,3) +
    b4.n * POW(2,4) +
    b5.n * POW(2,5) +
    b6.n * POW(2,6) +
    b7.n * POW(2,7) +
    b8.n * POW(2,8) +
    b9.n * POW(2,9) s
FROM
    binary_view b0,
    binary_view b1,
    binary_view b2,
    binary_view b3,
    binary_view b4,
    binary_view b5,
    binary_view b6,
    binary_view b7,
    binary_view b8,
    binary_view b9
HAVING s BETWEEN 1 AND 1000 ) t;
1
  • Could you possibly and a short explanation on what your code does?
    – Burgi
    May 8, 2016 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.