Creating Temporary Tables in SQL Using CREATE TABLE AS
When aiming to create a temporary table to select data based on a specific criterion, such as the register_type in your provided query, the correct syntax requires the use of the CREATE TABLE AS statement. In your case, the corrected query would be:
CREATE TEMP TABLE temp1 AS SELECT egauge.dataid, egauge.register_type, egauge.timestamp_localtime, egauge.read_value_avg FROM rawdata.egauge WHERE register_type LIKE '%gen%' ORDER BY dataid, timestamp_localtime;
Explanation:
Unlike the CREATE TABLE statement, CREATE TABLE AS performs both table creation and data insertion simultaneously. It allows you to create a temporary table that is a snapshot of the data at the time of creation. This temporary table is visible only within the current session and disappears automatically once the session ends.
Advantages of CREATE TABLE AS:
When ALTERNATE SYNTAXES WOULD BE USED:
The above is the detailed content of How Can I Efficiently Create a Temporary Table in SQL to Filter Data?. For more information, please follow other related articles on the PHP Chinese website!