Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking for boolean true / false result in postgresql query

I am running the following sql query in my web app:

SELECT EXISTS (
SELECT id
FROM user
WHERE membership=1244)

i was expecting true (boolean data) as the result but I'm getting 't' or 'f' for false. How do I get it to return to my lua code a standard boolean?

I found the following post:

Reading boolean correctly from Postgres by PHP

And so I tried to change my code to something like this:

SELECT EXISTS ::int (
SELECT id
FROM user
WHERE membership=1244)

or

SELECT ::INT (SELECT EXISTS (
SELECT id
FROM user
WHERE membership=1244))

But I'm getting a syntax error.
Can you tell the best way to handle this? Should I be casting the resulting 't' to a boolean somehow? or is there a way to tell postgresql to return true / false instead of 't'/'f'?

Thanks.

like image 279
mark li Avatar asked Jul 08 '26 21:07

mark li


2 Answers

You are so close

SELECT EXISTS (SELECT id FROM user WHERE membership=1244)::int 
like image 160
Barbara Laird Avatar answered Jul 11 '26 12:07

Barbara Laird


Your first query do indeed return a boolean. A t is shown as the returned value of the query

select exists (select 1);
 exists 
--------
 t

But if you check its type it is a boolean:

select pg_typeof(exists (select 1));
 pg_typeof 
-----------
 boolean

You will have to check with the lua's postgresql driver manual how to properly handle it.

like image 36
Clodoaldo Neto Avatar answered Jul 11 '26 13:07

Clodoaldo Neto