在MySQL中添加名为CURDATE()的列?

时间:2021-12-26 22:55:29

I'd like to add a column to existing table in MySQL with name = the day the addition is performed. And I'd like this to happen within the database(i.e. without php,python,perl scripts) I tried this :

我想在MySQL中的现有表中添加一列,其中name =执行添加的日期。我希望这发生在数据库中(即没有php,python,perl脚本)我试过这个:

SET @tmp_date = CURDATE();
ALTER TABLE my_report ADD @tmp_date INT(6);

But query fails with error #1064 - You have an error in your SQL syntax; It's not the date format because @tmp_date would return 2011-06-03 and

但查询失败,错误#1064 - 您的SQL语法中出错;这不是日期格式,因为@tmp_date将返回2011-06-03和

ALTER TABLE my_report ADD `2011-06-03` INT(6); 

executes ok

执行正常

2 个解决方案

#1


1  

set @query = concat("ALTER TABLE my_report ADD `", curdate(), "` INT(6)");
prepare stmt from @query;
execute stmt;

#2


2  

Add ` (backstick) around the column name:

在列名称周围添加`(backstick):

SET @tmp_date = CURDATE();
ALTER TABLE my_report ADD `@tmp_date` INT(6);

But this smells of bad design? Why do you create the new column and don't use another table, or similar structure?

但这有什么不好的设计气味?为什么要创建新列而不使用其他表或类似结构?

#1


1  

set @query = concat("ALTER TABLE my_report ADD `", curdate(), "` INT(6)");
prepare stmt from @query;
execute stmt;

#2


2  

Add ` (backstick) around the column name:

在列名称周围添加`(backstick):

SET @tmp_date = CURDATE();
ALTER TABLE my_report ADD `@tmp_date` INT(6);

But this smells of bad design? Why do you create the new column and don't use another table, or similar structure?

但这有什么不好的设计气味?为什么要创建新列而不使用其他表或类似结构?