postgresql,concat json values if not null

postgresql, concat json values if not null

我将 CONCAT 与来自 jsonb 列的值一起使用。 有时这些值之一可能是 null (json).

如何将整个返回字段 xywh 合并为 NULL?

query := `SELECT
              CONCAT( 
                data->'x',',',
                data->'y',',', 
                data->'w',',', 
                data->'h') as xywh 
              FROM table`

因为concat函数会忽略NULL

Concatenate all arguments. NULL arguments are ignored.

如果你想让结果是 NULL 如果任何 JSON 字段为空,我们可以尝试使用 || 连接字符串。

select  data->>'x' || ',' ||
        data->>'y' || ',' ||
        data->>'w' || ',' ||
        data->>'h'  
FROM table`