Rename Mysql Table name with concat

2019-09-17 08:25发布

In MySQL, I need to rename a table with a suffix which identifies previous month, and I'm trying to do that with this syntax:

RENAME TABLE 'myTable' TO CONCAT('myTable',DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y_%m'));

F.e: I've table name "customers" and what I want is "customer_2015_04".

The problem is with concat order.

What am I doing wrong?

Best regards,

1条回答
再贱就再见
2楼-- · 2019-09-17 09:13

I think you need to do this using a prepared statement:

set @sql = CONCAT('RENAME TABLE myTable TO myTable',
                  DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y_%m')
                 );

prepare s from @sql;

execute s;
查看更多
登录 后发表回答