关于mysql5.5数据库中变量的引用的问题.
1.有一个数据库是test_num_base,其中有一个test表.
我想通过变量的方式获取test的数据.
select * from test_num_base.test;
使用变量:
set @A=test_num_base;
但是再次访问使用: select * from @A.test;
报错:ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@A' at line 1
如果使用拼接的方法进行设置:
set @B=concat('test_num_base.'+'test');
访问: select * from @B;
还是报错.
求解释如何进行更正,小弟不甚感谢!
关于数据库变量的使用.
变量是不能在数据库名称和表名中出现,如果要使用的话,需要使用拼接语句.
set @name=test; #设置变量
set @sql=concat('select count(*) from ',@name,'.user'); #拼接语句
prepare dbpro from @sql; #预执行语句
execute dbpro; #执行结果