postgresql - 即使值存在,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。
解决方案
COALESCE('full','api')
等价于'full'
因为'full'
绝对不为空。
推荐阅读
- python-3.x - Pandas,DataFrame 来自几列的唯一值
- docker - Mac 上 Docker 中的各种缓存是否损坏?
- ios - 我如何在图像中创建 UIView?
- typescript - Angular 6:为什么即使不导入 HttpClientModule HttpClient 也能工作?
- typescript - 打字稿动态界面
- powershell - 将选择字符串管道结果与字符串进行比较
- node.js - Nodejs/express - Router.use() 需要中间件功能,但有一个 ' + gettype(fn)); 没有 app.use('/',routes);
- python - 如何克服'DataFrame'对象在Python的熊猫中没有属性'excelwriter'
- scikit-learn - 管道中 LogisticRegression 的 _coef 值过多
- getstream-io - 在 getstream 中代表分组的用户