Mysql
 SQL >> Database >  >> RDS >> Mysql

Usare un nome tabella dinamico in MySQL

Il modo pulito è usare le Prepared Statement. Per la concatenazione, MySQL usa concat:

SET @id := '47'; SET @table := concat(@id,'_2013_2014_voucher'); set @qry1:= concat('select * from ',@table); prepare stmt from @qry1; execute stmt;

Lo stesso pattern funziona anche per una DELETE o per qualsiasi statement in cui il nome tabella viene calcolato a runtime.