Left outer join - how to return a boolean for existence in the second table? Left outer join - how to return a boolean for existence in the second table? postgresql postgresql

Left outer join - how to return a boolean for existence in the second table?


"IS NULL" and "IS NOT NULL" return a boolean, so this should make it easy.

I think this is all you need?

SELECT                                 b.id IS NOT NULL as is_banned, -- The value of "is_banned" will be a boolean

Not sure if you need the "NOT" or not, but you'll get a bool either way.


A CASE or COALESCE statement with an outer join IS the proper way to do this.

select  CASE     WHEN b.id IS NULL THEN true    ELSE false  END AS banned,                            u.id,   u.first_name,   u.last_name,   u.city,  u.last_ip,  to_char(u.login, 'DD.MM.YYYY') as dayfrom pref_users u left outer join pref_ban2 b   on u.id=b.idlimit 10;