我需要检查表中的字段是否为空,并返回单独的结果/如果是,则执行不同的查询。这是一般的高级逻辑,但我不知道如何在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的记录,但master_user_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)