How do I use today's date as column heading in MySQL

3

I have the following table:

Consideringthattodayis02/01/2019,whenmakingthepivottablebelow:

SELECThora,CASEWHENdata=CURDATE()THENtarefaELSENULLENDAScampo_1FROMtabelaWHEREdataBETWEENCURDATE()ANDDATE_ADD(CURDATE(),INTERVAL2DAY)GROUPBYhora,dataORDERBYhora

Igetthefollowingoutput:

ButI'dliketoget:

All help is welcome, thank you

    
asked by anonymous 03.01.2019 / 20:42

1 answer

1

You can do it this way:

SET @curdate = CURDATE();
SET @sql = CONCAT('SELECT hora,
       CASE WHEN data = CURDATE() 
          THEN tarefa 
          ELSE NULL 
       END AS "', @curdate, '"
  FROM tabela
 WHERE data BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 2 DAY)
 GROUP BY hora, data 
 ORDER BY hora');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

So you put the current date first on a variable and then run the query.

A small example

Font

    
03.01.2019 / 21:02