O'Reilly logo

MySQL 8 Cookbook by Karthik Appigatla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Saving as a table

You can save the results of a SELECT statement into a table. Even if the table does not exist, you can use CREATE and SELECT to create the table and load the data. If the table already exists, you can use INSERT and SELECT to load the data.

You can save the titles into a new titles_only table:

mysql> CREATE TABLE titles_only AS SELECT DISTINCT title FROM titles;Query OK, 7 rows affected (0.50 sec)Records: 7  Duplicates: 0  Warnings: 0

If the table already exists, you can use the INSERT INTO SELECT statement:

mysql> INSERT INTO titles_only SELECT DISTINCT title FROM titles;Query OK, 7 rows affected (0.46 sec)Records: 7  Duplicates: 0  Warnings: 0

To avoid duplicates, you can use INSERT IGNORE. However, in this case, there is ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required