Alex Rivera | Logout

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

Asked 2010-07-12T11:34:28.517
13

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 true for a 0 difference otherwise return false. I don't know how to return a value.

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

Edit
Report

2 Answers

23

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.

You could write your query as:

SELECT (a = b) AS a_equals_b
FROM emp
WHERE ...
answered 2010-07-12T11:37:14.497
14
select a, b, if(a-b=0, true, false) as diff from emp;
answered 2010-07-12T11:40:12.443

Your Answer