简体   繁体   English

MySQL变量(使用声明)

[英]MySQL variables (USE STATEMENT)

I have set MySQL variable: 我设置了MySQL变量:

SET @test = 'my_db';
select @test; 

Then 然后

use  @test
ERROR 1049 (42000): Unknown database '@test'

OR 要么

mysql> SET @test = 'my_db';
Query OK, 0 rows affected (0.00 sec)

mysql> select @test;
+-------+
| @test |
+-------+
| my_db   |
+-------+
1 row in set (0.00 sec)

mysql> use @test
ERROR 1049 (42000): Unknown database '@test'

How I can use variable value with use USE STATEMENT in MySQL? 如何在MySQL中使用USE STATEMENT来使用变量值?

Variables cannot be used as identifiers in MySQL. 变量不能在MySQL中用作标识符。 You're looking for macro expansion, which MySQL doesn't support. 您正在寻找MySQL不支持的宏扩展。

User variables are intended to provide data values. 用户变量旨在提供数据值。 They cannot be used directly in an SQL statement as an identifier or as part of an identifier, such as in contexts where a table or database name is expected, or as a reserved word such as SELECT 它们不能直接在SQL语句中用作标识符或标识符的一部分,例如在需要表或数据库名称的上下文中,或作为SELECT等保留字

From http://dev.mysql.com/doc/refman/5.6/en/user-variables.html 来自http://dev.mysql.com/doc/refman/5.6/en/user-variables.html

It is possible to work around this for some statements such as SELECT by using prepared statements, but you cannot prepare a USE statement, so it's not an option for you in this case. 可以解决此对于一些语句,如SELECT通过使用预处理语句,但你不能准备一个USE语句,所以它不是在这种情况下,你的一个选择。

You'll have to select the database in your application code by using mysqli_select_db($test) or by executing a query such as mysqli_query("USE $test"); 您必须使用mysqli_select_db($test)或执行诸如mysqli_query("USE $test");类的查询来在应用程序代码中选择数据库mysqli_query("USE $test");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM