Marc Marc - 6 months ago 10
SQL Question

Create a temporay table with more columns than specified in the SELECT statement

I have created a temporary table from another table and it works well. However, I'd like to add a column to the temporary table and then SELECT ALL from the existing table(unoone). Please any insight and thanks upfront

$sql="CREATE TEMPORARY TABLE IF NOT EXISTS unoone_two AS (SELECT * FROM unoone)";


enter code here

Answer

You can add the columns to the select:

CREATE TEMPORARY TABLE IF NOT EXISTS unoone_two AS 
    SELECT u.*, NULL as newcol, CAST(NULL as SIGNED) as newcol2
    FROM unoone u;

You can specify a type by using CAST().