SQL

Saving a query result in a new table


Problem

Sometimes when you query a table you want to keep the result in another table so that you can make more changes on it, or just keep it as a reference.



Solution

It's fairly simple, you basically use CREATE TABLE, but instead of specifying columns you append the query of your choice and MySQL will automatically store the result in a table:

CREATE TABLE new_table AS SELECT column, 1 as num_column FROM old_table

Comments




Please sign-in to post a comment