当具有 NULL 值时,我在生成查询以将 JSON 数组显示为一组文本值时遇到问题。假设我有下表:
Name | Meta
Art0 | {"category":["sport"]}
Art1 | [NULL]
Art2 | {"category":["sport", "health"]}
Run Code Online (Sandbox Code Playgroud)
如果我做类似的事情:
SELECT name, jsonb_array_elements_text(meta->'category') tag FROM table
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
Name | Tag
Art0 | sport
Art2 | sport
Art2 | health
Run Code Online (Sandbox Code Playgroud)
问题是 Art1 正在被删除。如何执行还包含 Art1 行以及 Tag 列上的空字符串或 NULL 值的查询?
谢谢
并且没有join/ union:
SELECT
name,
jsonb_array_elements_text(coalesce(meta->'category', '[null]')) tag
FROM table;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1618 次 |
| 最近记录: |