MySQL - Create temporary table according to dynamic SELECT

1

Is there any way to create a temporary table according to a select with columns created dynamically, as shown in the examples below?

ex:

SELECT idCentroCusto, 'Dez/14', 'Jan/15', 'Fev/15', 'Mar/15', totalAnual

or

SELECT idCentroCusto, 'Dez/14', 'Jan/15', 'Fev/15', 'Mar/15', 'Abr/15', 'Mai/15', totalAnual

Since (as shown above) these Months / Year are dynamically created, according to the calendar parameter.

    
asked by anonymous 03.03.2015 / 14:57

2 answers

2

See the CREATE TABLE documentation, which can be created through a query:

CREATE TEMPORARY TABLE tabela_temporaria SELECT idCentroCusto, 'Dez/14', 'Jan/15', 'Fev/15', 'Mar/15', totalAnual;
    
03.03.2015 / 15:03
0

Try using the "CREATE TABLE AS" command from MySQL

 CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (SELECT idCentroCusto, 'Dez/14', 'Jan/15', 'Fev/15', 'Mar/15', 'Abr/15', 'Mai/15', totalAnual FROM table1)

Source: link

    
03.03.2015 / 15:00