O'Reilly logo

Sams Teach Yourself MySQL by Chris Newman

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

Using a Query to Create a Table

You can use the result of a query to create a new table by putting a SELECT statement inside the CREATE TABLE command.

The following example creates a table that contains a subset of the customer_contacts table for one customer. This is also created as a temporary table, so it will be destroyed when you end your MySQL sessions.

mysql> CREATE TEMPORARY TABLE scicorp_customers AS
    -> SELECT * FROM customer_contacts
    -> WHERE customer_code ='SCICORP';
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

The keyword AS is optional but can be included to make the command more readable. If you view the structure of the new table, you will see that all the columns from customer_contacts are present. ...

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