如何选择表名为Variable的MySQL

时间:2012-01-10 20:19:01

标签: mysql set tablename

我有一种情况,获取表名应该来自一个设置变量,如:

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中的宝贝步骤。

2 个答案:

答案 0 :(得分:71)

您必须使用prepared statement执行此操作。类似的东西:

SET @s = CONCAT('select * from ', @Cat, ' where ID = ', @ID_1); 

PREPARE stmt1 FROM @s; 
EXECUTE stmt1; 
DEALLOCATE PREPARE stmt1; 

答案 1 :(得分:-2)

I got this solution after hours of debugging

我为mysql安装的图像100%