postgresql postgres 中的 sqlite IFNULL()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43934155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:26:14  来源:igfitidea点击:

sqlite IFNULL() in postgres

postgresqlsqliteifnull

提问by RagnarLodbrok

What is the equivalent of SQLite's IFNULL()in Postgres?

IFNULL()Postgres中 SQLite 的等价物是什么?

I have to following query (sqlite in Ruby):

我必须遵循查询(Ruby 中的 sqlite):

SELECT ifnull(max(code_id) + 1, 1) 
FROM configentries 
WHERE configtable_id = ...

How should this look like if I want the same result with PostgreSQL?

如果我想要与 PostgreSQL 相同的结果,这应该如何?

回答by Vao Tsun

trycoalesce:

尝试coalesce

The COALESCE function returns the first of its arguments that is not null. Null is returned only if all arguments are null

COALESCE 函数返回它的第一个不为空的参数。仅当所有参数都为 null 时才返回 Null

SELECT coalesce(max(code_id) + 1, 1) 
FROM configentries 
WHERE configtable_id = ...

回答by rurugg

Try this, Select NULLIF(Max(code_id), 0) +1 from configentries WHERE configtable_id = ...

试试这个,选择 NULLIF(Max(code_id), 0) +1 from configentries WHERE configtable_id = ...