如何从 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
How to return a boolean value from a function in MySQL?
提问by ratty
I have column a
and column b
in 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 a
和column 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. TRUE
and FALSE
are aliases to 1 and 0, and the BOOL
column type is just an alias for TINYINT(1)
. All expressions that appear to give boolean results actually return 0 or 1.
MySQL 并没有真正的布尔值。TRUE
和FALSE
是 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;
回答by Flakron Bytyqi
go to
去
http://dev.mysql.com/doc/refman/5.1/en/numeric-type-overview.html
http://dev.mysql.com/doc/refman/5.1/en/numeric-type-overview.html
section
部分
BOOL, BOOLEAN
布尔值,布尔值