提示用户在 MySQL 中输入一个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21885940/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:05:16  来源:igfitidea点击:

Prompt user to input a variable in MySQL

mysqlsqloraclevariablesoracle-sqldeveloper

提问by Roreo

At school, I believe I work with Oracle SQL Developer when writing SQL. And in this I can type:

在学校,我相信我在编写 SQL 时使用 Oracle SQL Developer。在这个我可以输入:

SELECT Book_Title, Auth_ID
FROM book
WHERE Auth_ID = '&Enter ID';

This will then display a little message box where the user can enter an ID number to see all the books written by an author with that ID number.

这将显示一个小消息框,用户可以在其中输入 ID 号以查看具有该 ID 号的作者所写的所有书籍。

I want to know if there is a way to do this in MySQL. I have looked and the nearest thing I can find is setting a variable before hand, which is not quite what I'm looking for:

我想知道在 MySQL 中是否有办法做到这一点。我已经看过了,我能找到的最近的事情是事先设置一个变量,这不是我想要的:

SET @EnterID := 2;
select Book_Title, Auth_ID
from book
where Auth_ID = @EnterID;

The above statement in MySQL willreturn all the books with author ID of 2, but only because I set it to that previously. I want the user to be able to enter the variable.

MySQL 中的上述语句返回作者 ID 为 2 的所有书籍,但这仅仅是因为我之前将其设置为该值。我希望用户能够输入变量。

Thanks.

谢谢。

回答by hbpatrick81

For a prompt, you must put the char ':' followed by the name of the variable

对于提示,您必须将字符 ':' 后跟变量名称

Example :

例子 :

select * 
from YOUR_TABLE 
where YOUR_COLUMN = :your_var

回答by tHiNk_OuT_oF_bOx

mysql is to run SQL queries .SQL is a query language, it is not for user interaction

mysql是运行SQL查询的。SQL是一种查询语言,不是为了用户交互

See : How to ask MySQL to prompt for values in a query?

请参阅:如何要求 MySQL 在查询中提示输入值?