Quick and easy INSERT’s in MySQL with the SELECT statement.

The SELECT clause has more uses than just returning those columns you specify in your query. Want to see a handy use for it? Need to populate a table with values from another table? But don’t want to do it by hand? You are in luck…

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.
OS and DB used:

  • Xubuntu Linux 16.04.5 LTS (Xenial Xerus)
  • MySQL 5.7.24

I have these query results:

1
2
3
4
5
6
7
8
9
10
11
mysql> SELECT *
    -> FROM cell_phone_storage
    -> WHERE phone_number IS NOT NULL;
+----+--------+--------------+
| id | f_name | phone_number |
+----+--------+--------------+
|  1 | Josh   | 222-333-4444 |
|  2 | Mark   | 888-888-0000 |
|  3 | Kim    | 444-111-5555 |
+----+--------+--------------+
3 rows in set (0.01 sec)

brown-paper-clipped-to-larger-white-paper-on-folder

Photo by Kelly Sikkema on Unsplash

And I want to store them in a similar table, such as this one:

1
2
3
4
5
6
7
8
9
mysql> DESC cell_phone_2;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id          | int(11)     | NO   | PRI | NULL    |       |
| f_name      | varchar(25) | NO   | MUL | NULL    |       |
| cell_number | char(12)    | NO   |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.04 sec)

No need to make things difficult. Hard coding those results individually in a VALUES clause is extra, unnecessary work.

You can use SELECT instead:

1
2
3
4
5
6
mysql> INSERT INTO cell_phone_2
    -> SELECT *
    -> FROM cell_phone_storage
    -> WHERE phone_number IS NOT NULL;
Query OK, 3 rows affected (0.39 sec)
Records: 3  Duplicates: 0  Warnings: 0

And there you have it. Easy as pie.

1
2
3
4
5
6
7
8
9
mysql> SELECT * FROM cell_phone_2;
+----+--------+--------------+
| id | f_name | cell_number  |
+----+--------+--------------+
|  1 | Josh   | 222-333-4444 |
|  3 | Kim    | 444-111-5555 |
|  2 | Mark   | 888-888-0000 |
+----+--------+--------------+
3 rows in set (0.00 sec)

To note: For this to work, the number of columns in your query, along with the data type(s), must match that of the target table.

Like what you have read? See anything incorrect? Please share your thoughts and comments below. Thanks for reading!!!

Explore the official MySQL 5.7 Online Manual for more information.

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, are performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Advertisements

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.