Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

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?

like image 861
ratty Avatar asked Jul 12 '10 11:07

ratty


2 Answers

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 ...
like image 86
Mark Byers Avatar answered Sep 24 '22 21:09

Mark Byers


select a, b, if(a-b=0, true, false) as diff from emp;
like image 33
Salil Avatar answered Sep 20 '22 21:09

Salil