本文介绍了如何从 MySQL 中选择表名是可变的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种情况,应该从一个集合变量中获取表名,例如:
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 中选择表名是可变的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!