具有多个条件的 MySQL If 语句

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

MySQL If statement with multiple conditions

mysql

提问by avinash chavan

I want to write IFstatement inside a Stored procedurein MySQL in the following way:

我想通过以下方式在 MySQL 中的IFaStored procedure中编写语句:

IF (exp1 and exp2 and exp3) or exp4

I know MySQL will treat IF()as a function call. But I hope you got what I'm trying to achieve. I am new to MySQL syntax.

我知道 MySQL 会将其IF()视为函数调用。但我希望你得到了我想要实现的目标。我是 MySQL 语法的新手。

回答by Jorge Campos

In a procedure the use of an IFis pretty straight forward:

在一个过程中, an 的使用IF非常简单:

IF (yourCondition [logical operator(OR, AND) another condition] ) THEN

So in a practical example:

所以在一个实际的例子中:

....
DECLARE m integer;
DECLARE n integer;
SET m = 1;
SET n = 0;
IF ((m>n AND m=1 AND n=0) OR m=n)THEN
     some code here
END IF;

The evaluation of the conditions follows the parenthesis rule same as in a mathematical operation.

条件的评估遵循与数学运算中相同的括号规则。

You can refer to the Docs

你可以参考文档