How to insert into same table in MySQL?


Sometimes you may need to quickly create lot of data for load testing. You can select and insert into same table in MySQL. This approach rapidly grows your existing tables. It gives a lot of records to play with. Here are a few ways to do it.

Insert into same table in MySQL

Syntax

INSERT INTO table_name ( field1, field2,...fieldN )
                       SELECT field1, field2, fieldN from table_name;

Example
For a table table_name with columns a, b, c

+------+------+------+
|   a  |   b  |   c  |
+------+------+------+
|   1  |   2  |   3  |
|   4  |   5  |   6  |
|   7  |   8  |   9  |
|   3  |   3  |   3  |
+------+------+------+

Insert into same table in MySQL all records from a table

mysql> INSERT INTO table_name (a,b,c) select a,b,c from table_name;
+------+------+------+
|   a  |   b  |   c  |
+------+------+------+
|   1  |   2  |   3  |
|   4  |   5  |   6  |
|   7  |   8  |   9  |
|   3  |   3  |   3  |
|   1  |   2  |   3  |
|   4  |   5  |   6  |
|   7  |   8  |   9  |
|   3  |   3  |   3  |
+------+------+------+

Insert into same table in MySQL some records from a table

mysql> INSERT INTO table_name (a,b,c) select a,b,c from table_name where a=1 or a=3;
+------+------+------+
|   a  |   b  |   c  |
+------+------+------+
|   1  |   2  |   3  |
|   4  |   5  |   6  |
|   7  |   8  |   9  |
|   3  |   3  |   3  |
|   1  |   2  |   3  |
|   3  |   3  |   3  |
+------+------+------+

You may get a duplicate value error if one of these fields is a primary key or you have a uniqueness constraint for a field. Here’s how you can avoid it.

Insert into same table in MySQL without duplicates

Syntax

INSERT INTO table_name ( field1, field2,...fieldN )
                       SELECT field1, field2, fieldN from table_name
                       ON DUPLICATE KEY
                       UPDATE primary_key_field=<expression> ;

Example

For a table table_name with columns a, b, c and c is primary key

+------+------+------+
|   a  |   b  |   c  |
+------+------+------+
|   1  |   2  |   3  |
|   4  |   5  |   6  |
|   7  |   8  |   9  |
+------+------+------+

Insert into same table in MySQL all records from a table

mysql> INSERT INTO table_name (a,b,c) select a,b,c from table_name ON DUPLICATE KEY c=c+1;
+------+------+------+
|   a  |   b  |   c  |
+------+------+------+
|   1  |   2  |   3  |
|   4  |   5  |   6  |
|   7  |   8  |   9  |
|   1  |   2  |   4  |
|   4  |   5  |   7  |
|   7  |   8  |  10  |
+------+------+------+

Insert into same table in MySQL some records from a table

mysql> INSERT INTO table_name (a,b,c) select a,b,c from table_name where a=1 or a=3 ON DUPLICATE KEY c=c+1;
+------+------+------+
|   a  |   b  |   c  |
+------+------+------+
|   1  |   2  |   3  |
|   4  |   5  |   6  |
|   7  |   8  |   9  |
|   1  |   2  |   5  |
+------+------+------+

You can read more about How to do a bulk insert in MySQL

mm

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!