我有一种情况,应该从一个集合变量中获取表名,例如:
I have a case where getting the table name should be from a set variable like:
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 中的小步骤.
but doing that way MySQL outputs an error, so could someone show me how I can achieve that, because these are my baby steps in MySQL.
你必须使用 准备好的语句.类似的东西:
You'd have to do this with a prepared statement. Something like:
SET @s = CONCAT('select * from ', @Cat, ' where ID = ', @ID_1);
PREPARE stmt1 FROM @s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
这篇关于如何从 MySQL 中选择表名是可变的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!