Postgresql 为条件逻辑postgres返回单独的查询

Postgresql 为条件逻辑postgres返回单独的查询,postgresql,Postgresql,我需要检查表中的字段是否为空,并返回单独的结果/如果为空,则执行不同的查询。这是一般的高级逻辑,但我不知道如何在Postgres中运行它 CASE WHEN (select master_user_id from users where id = 88 IS NULL) THEN select company_name from users where id = 88; ELSE select company_name from users where id = (sel

我需要检查表中的字段是否为空,并返回单独的结果/如果为空,则执行不同的查询。这是一般的高级逻辑,但我不知道如何在Postgres中运行它

CASE
  WHEN (select master_user_id from users where id = 88 IS NULL) THEN
    select company_name from users where id = 88;
  ELSE
    select company_name from users where id = (select master_user_id from users where id = 88);
END CASE;

如果始终存在id=88但主用户id为空的记录,则可以按如下方式编写逻辑:

select company_name from users 
where id IN (select COALESCE(master_user_id,id) from users where id = 88 )
如果您正在检查是否存在记录id=88,则:

select company_name from users 
where id IN COALESCE((select master_user_id from users where id = 88),88)  

太好了,谢谢你。如何执行此操作以返回多个查询结果。i、 e.对id使用IN从id=88的用户中选择公司名称(主用户id,id)是,如果子查询返回多条记录,您应该在中使用