Rename Mysql Table name with concat

老子叫甜甜 提交于 2020-01-06 14:07:37

问题


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:


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;


来源:https://stackoverflow.com/questions/30236515/rename-mysql-table-name-with-concat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!