MySQL SELECT COUNT> 0 as a boolean

I am trying to get a true / false value in a column in a select expression that indicates whether count> 0 or not.

I read these documents: http://dev.mysql.com/doc/refman/5.1/en/expressions.html

Which I don’t quite understand, but they seem to indicate that I can use “→” in my expression.

So, I tried this:

SELECT COUNT(*)>>0 AS my_bool FROM table GROUP BY id;

It starts and does not generate errors or warnings, but the column my_boolcontains only the results COUNT(*). This is what I'm trying to do, if so, how?

PS Yes, I know that I could just check x> y in my code that processes the results, I want to know if this could be something that can only be done in MySQL (feel free to explain why this is not practical, everyone means)

+5
source share
5 answers

In your example, you are not using an operator greater than, but << 21>. If you use

SELECT COUNT(*) > 0 AS my_bool FROM table GROUP BY id;

my_bool will contain 0 or 1 as a boolean.

+16
source

Use the if condition to check and return true / false.

Example:

SELECT if(COUNT(*)>0,'true','false') AS my_bool FROM table GROUP BY id;

If it is checked that if count is greater than 0, then return true else return false.

+4
source

:

SELECT (COUNT(*) <> 0) AS my_bool 
FROM table 
GROUP BY id;
+1

, , , :

SELECT (
    SELECT COUNT(*) AS my_bool FROM table GROUP BY id
 ) != 0;

COUNT(*) , 0, 1.

+1

-

SET @countValue = (SELECT COUNT(*) FROM table GROUP BY id);  
if @countValue > 0
    return 1;
else
    return 0;
+1

All Articles