我正在尝试执行这样的mysql查询
SET @id := '47'; SET @table := @id+'_2013_2014_voucher'; SELECT * FROM @table; Delete FROM @table where id=@id
它显示这样的错误
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@table' at line 1
我该如何实现?
在查询中动态表名的使用最好与 Prepared Staments一起使用 ,在mysql中也可以使用串联功能concat
concat
SET @id := '47'; SET @table := concat(@id,'_2013_2014_voucher'); set @qry1:= concat('select * from ',@table); prepare stmt from @qry1 ; execute stmt ;
您也可以针对删除查询执行此操作