tags:

views:

69

answers:

1

How can MySQL insert multiple records by executing a single insert statement?

The problem at hand involves 1 to 10 records, depending upon user input.

+5  A: 

Just separate the values by comma.

INSERT INTO
    tablename (colname1, colname2, colname3)
VALUES 
    ('foo1', 'bar1', 'waa1'), 
    ('foo2', 'bar2', 'waa2'), 
    ('foo3', 'bar3', 'waa3')
BalusC
but this is fixed for 3 records but what about if no. of record are dynamic? from 1 record to 10??
nectar
Just generate the sql dynamically using the same programming language as you're gathering the user input? Alternatively, you can also use "batch inserts". The details however depends on the programming language in question. If it was for example Java, then I would suggest `PreparedStatement#addBatch()` for this. If it was PHP, then `implode()` may come handy.
BalusC