Home >Database >Mysql Tutorial >Can I Create a Temporary Table from a SELECT Statement Without Explicit Column Definitions?
Query:
Can a temporary (session-only) table be created solely from a SELECT statement, without relying on a separate CREATE TABLE statement and tedious column type specification?
Explanation:
Temporary tables are often useful for storing intermediate data during database operations. Creating them using a CREATE TABLE command requires manually specifying each column and its data type, which can be time-consuming.
Answer:
Yes, it is possible to create a temporary table directly from a SELECT statement using the following syntax:
CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (SELECT * FROM table1)
This command accomplishes the following:
This method saves time by avoiding the need to write separate CREATE TABLE and column definition statements, while still providing the benefits of temporary table usage.
The above is the detailed content of Can I Create a Temporary Table from a SELECT Statement Without Explicit Column Definitions?. For more information, please follow other related articles on the PHP Chinese website!