首页 > 解决方案 > 即使值存在,PostgreSQL COALESCE 函数也会返回 Null

问题描述

我在 PostgreSQL 数据库中有下表(节点),我正在寻找提取 http 或 https 信息。我正在使用 COALESCE。

SELECT COALESCE(http,https) 
FROM oig.nodes 
WHERE owner_name = %s 
  AND node_type = COALESCE('full','api')
|---------------------|------------------|------------------|------------------|
|      owner          |     node_type    |       http       |      https       |
|---------------------|------------------|------------------|------------------|
|          test123    |         full     | http://1.1.1.1   |                  |
|---------------------|------------------|------------------|------------------|
|          testabc    |         api      |                  | http://1.1.1.2   |
|---------------------|------------------|------------------|------------------|
|          testabc    |         seed     |                  |                  |
|---------------------|------------------|------------------|------------------|

这有效,但仅当 node_type = full 时,如果 node_type = api,它返回 None。

标签: postgresql

解决方案


COALESCE('full','api')等价于'full'因为'full'绝对不为空。


推荐阅读