我有一种情况,从中获取表名应该是从一个设置变量:
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;