我有一张 table Image
id | path | additional | item_id
1 /url/.. {"sm":"/url2/", "md": "/url3/"} 1
additional
是一个 jsonb,可以是 Null、空 {} 或有值
如果像 sm
这样的键存在,我想检查 jsonb 。如果键存在则返回值如果不存在则返回路径
。
我最初的想法是使用 CASE
但我在检查和从数组中检索值时遇到问题
最佳答案
使用 coalesce()
:
select id,
coalesce(additional ->> 'sm', path) as path
from image;
如果键不存在,运算符 ->>
将返回 null
。
关于postgresql - 如果主要请求字段为 None 或为空,则使用条件 (CASE) 从另一个字段检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55535483/