如何从 MySQL 中的函数返回布尔值?

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

How to return a boolean value from a function in MySQL?

mysql

提问by ratty

I have column aand column bin table emp. I'd like to retrieve values from those columns and find the difference between them with a function. The function would return truefor a 0 difference otherwise return false. I don't know how to return a value.

我有column acolumn b在表中emp。我想从这些列中检索值并使用函数找到它们之间的差异。对于 0 差异,该函数将返回true,否则返回false。我不知道如何返回一个值。

Also, how do I store the retrieved values in a variable?

另外,如何将检索到的值存储在变量中?

回答by Mark Byers

MySQL doesn't really have booleans. TRUEand FALSEare aliases to 1 and 0, and the BOOLcolumn type is just an alias for TINYINT(1). All expressions that appear to give boolean results actually return 0 or 1.

MySQL 并没有真正的布尔值。TRUEFALSE是 1 和 0BOOL的别名,列类型只是TINYINT(1). 所有看似给出布尔结果的表达式实际上都返回 0 或 1。

You could write your query as:

您可以将查询写为:

SELECT (a = b) AS a_equals_b
FROM emp
WHERE ...

回答by Salil

select a, b, if(a-b=0, true, false) as diff from emp;