In MySQL add column with name CURDATE()?
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 :
SET @tmp_date = CURDATE();
ALTER TABLE my_report ADD @tmp_date INT(6);
But query fails wit开发者_Go百科h 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
ALTER TABLE my_report ADD `2011-06-03` INT(6);
executes ok
Add ` (backstick) around the column name:
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?
set @query = concat("ALTER TABLE my_report ADD `", curdate(), "` INT(6)");
prepare stmt from @query;
execute stmt;
精彩评论