MySQL:带有prepare命令的命名参数? [英] MySQL : named parameters with prepare command?
问题描述
是否可以在 MySql 中使用 prepare 命令和 命名参数,例如 PHP 中的 PDO:
Is it possible in MySql to use the prepare command with named parameters such as PDO in PHP:
这是我的例子:
SET @s = 'SELECT * FROM MY_TABLE WHERE my_column_1 = ? AND my_column_2 = ? ';
PREPARE stmt2 FROM @s;
SET @a = 54;
SET @b = 89';
EXECUTE stmt2 USING @a, @b;
是否可以做这样的事情:
Is it possible to do something like that :
SET @s = 'SELECT * FROM MY_TABLE WHERE my_column_1 = :value1 AND my_column_2 = :value2 ';
推荐答案
我建议查看有关此的文档.https://dev.mysql.com/doc/refman/8.0/zh/prepare.html
I suggest looking at the documentation regarding this. https://dev.mysql.com/doc/refman/8.0/en/prepare.html
该文档没有提及除 ?
之外的任何其他绑定变量的方法,但它确实提到您可以使用用户定义的变量.
The documentation makes no references to any other way to bind variables other than the ?
s, but it does mention that you can use user defined variables.
SET @s = 'SELECT * FROM MY_TABLE WHERE my_column_1 = @a AND my_column_2 = @b ';
PREPARE stmt2 FROM @s;
SET @a = 54;
SET @b = 89';
EXECUTE stmt2;
产生相同的输出并且变量只在执行语句时被评估,它只是缺乏将变量绑定到查询的明确性.
Produces the same output and the variables are only evaluated at execution of the statement, it just lacks the explicitness of binding the variable to the query.
这篇关于MySQL:带有prepare命令的命名参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!