执行变量中存在的 mysql 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10122815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
execute mysql query present in variable
提问by Avinash
I have a MySQLvariable
我有一个 MySQL 变量
@query = CONCAT('INSERT INTO history VALUES (',1,',',50,',UTC_TIMESTAMP()');
I want to execute the insert statement present in the variable.
我想执行变量中存在的插入语句。
回答by mattytommo
You must first prepare the statement using PREPARE
and then use EXECUTE
您必须首先使用准备语句PREPARE
,然后使用EXECUTE
See here: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
见这里:http: //dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
Something like:
就像是:
SET @query = CONCAT("INSERT INTO history VALUES (",1,",",50,",UTC_TIMESTAMP()");
PREPARE stmt1 FROM @query;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
回答by Ankit Sharma
Set @query = CONCAT("INSERT INTO history VALUES (",1,",",50,",UTC_TIMESTAMP()");
and then
进而
PREPARE stmt_name FROM @query
and at last
最后
EXECUTE stmt_name
回答by Rui Marques
remove the CONCAT(), it's not doing anything except generate an error since it's trying to concatenate a string with nothing else.
删除 CONCAT(),它除了生成错误之外什么都不做,因为它试图将字符串与其他任何东西连接起来。