0

lets say I have 2 tables :

group
{
     id,
     export boolean not null default false
}

and

channel
{
     id,
     group_id null,
     export boolean null
}

Is it possible, with 1 query, to request the export boolean value for a specified channel id such as :

  • channel.export is not null return it as is
  • if channel.export is null and channel.group_id is null return false
  • if channel.export is null and channel.group_d is not null return group.export

I am using postgresql 10.

This seems a little bit hard.

1

You requirement can be translated into a CASE statement expression almost word for word.

SELECT 
CASE 
    WHEN channel.export is not null then channel.export 
    WHEN channel.group_id is null then false
    ELSE "group".export
END as merged_bool
FROM channel LEFT JOIN "group" ON (group_id="group".id);
  • great ! thank you – iXô Jun 17 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.