使用条件 (CASE) 从另一个字段中检索值(如果主请求字段为 None 或为空)



我有一个表格Image

id  | path      | additional                       | item_id 
1     /url/..     {"sm":"/url2/", "md": "/url3/"}            1     

additional 是一个 jsonb,可以是 Null、空 {} 或带有值

我想检查 jsonb 是否存在像sm这样的键。如果键存在,则返回值,如果不返回path

我的起始想法是使用CASE但我在检查和从数组中检索值时遇到问题

使用 coalesce()

select id, 
       coalesce(additional ->> 'sm', path) as path
from image;

如果密钥不存在,运算符->>将返回null