如何从MySQL中select表名是可变的

我有一个情况,获取表名称应该从一个设置variables,如:

SET @ID_1 = (SELECT ID FROM `slider` LIMIT 0,1); SET @Cat = (SELECT Category FROM `slider` LIMIT 0,1); select * from @Cat where ID = @ID_1 

但这样做MySQL的输出和错误,所以有人可以指出我如何实现,因为这些是我的宝贝步骤MySQL

你必须做一个准备好的声明 。 就像是:

 SET @s = CONCAT('select * from ', @Cat, ' where ID = ', @ID_1); PREPARE stmt1 FROM @s; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; 
Interesting Posts