我正在尝试使用基于当前年份和月份(2011-09
)的名称创建一个表,但MySQL似乎不喜欢这样。
SET @yyyy_mm=Year(NOW())+'-'+Month(NOW());
CREATE TABLE `survey`.`@yyyy_mm` LIKE `survey`.`interim`;
SHOW TABLES IN `survey`;
+-----------+
| interim |
+-----------+
| @yyyy_mm |
+-----------+
如果CREATE TABLE;
@yyyy_mm
没有勾选@yyyy_mm
,我会收到一般语法错误。
2020
解析为{{1}}。
答案 0 :(得分:29)
你应该可以这样做:
SET @yyyy_mm=DATE_FORMAT(now(),'%Y-%m');
SET @c = CONCAT('CREATE TABLE `survey`.`',@yyyy_mm, '` LIKE `survey`.`interim`');
PREPARE stmt from @c;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
答案 1 :(得分:6)
set @yyyy_mm=concat(year(now()),'-',month(now()));
set @str = concat('create table survery.`', @yyyy_mm,'` like survey.interim;');
prepare stmt from @str;
execute stmt;
deallocate prepare stmt;