P粉8212313192023-08-03 09:31:45
Tables cannot contain blank or missing column names, while result sets can. The CREATE TABLE SELECT statement obtains the column names of the table from the result set of the SELECT statement. Therefore, you need to ensure that each column of the result set has a name, either derived from a column in some table or specified via an AS alias. For example,
SELECT 'one' AS tablename, cola, colb, colc FROM table_one UNION ALL SELECT 'two', cola, colb, colc FROM table_two UNION ALL SELECT 'three', cola, colb, colc FROM table_three
applies to the CREATE TABLE statement, while
SELECT 'one', cola, colb, colc FROM table_one UNION ALL SELECT 'two', cola, colb, colc FROM table_two UNION ALL SELECT 'three', cola, colb, colc FROM table_three
does not apply because it does not assign an alias column name to the first column.