Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL SELECT COUNT > 0 as boolean value

Tags:

mysql

I'm trying to get a true/false value in a column in a select statement which indicates if a count is >0 or not.

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

Which I don't fully understand but they seemed to indicate I could use '>>' in my expression.

So I tried this:

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

It runs and doesnt generate errors or warnings, however the my_bool column just contains the results of the COUNT(*). Is what I'm trying to do possible, if so how?

P.S Yes I know I could just test for x>y in my code that handles the results, I want to know if it is something that can be done in MySQL alone (feel free to explain why it's not advisable, by all means)

like image 324
totallyNotLizards Avatar asked May 01 '13 09:05

totallyNotLizards


1 Answers

In your example you are not using the greater than operator, but a bit shift (>>). If you use

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

my_bool will contain 0 or 1 as boolean value.

like image 109
Gene Vincent Avatar answered Oct 10 '22 09:10

Gene Vincent