📅  最后修改于: 2023-12-03 15:01:22.363000             🧑  作者: Mango
In PostgreSQL, the IFNULL
function is not available. However, there is an equivalent function called COALESCE
.
The COALESCE
function returns the first non-null value in a list of arguments. The syntax for the function is as follows:
COALESCE(value1, value2, ..., valueN)
value1
, value2
,...,valueN
are the values to check for null. If all values are null, then the function returns null.
SELECT COALESCE(NULL, 'hello', NULL, 'world', NULL); -- Output: 'hello'
SELECT COALESCE(NULL, NULL, NULL); -- Output: NULL
The COALESCE
function can be used in many ways, such as:
SELECT COALESCE(name, email, phone, 'N/A') AS contact
FROM users;
The above query will return a list of contacts from users
table. If name
, email
, or phone
is null, then N/A
will be returned as the contact.
UPDATE users
SET name = COALESCE(name, 'User'),
email = COALESCE(email, 'user@example.com'),
phone = COALESCE(phone, '000-000-0000');
The above query will update the users
table with non-null values for name
, email
, and phone
. If any of these values are null, then default values will be used.
In PostgreSQL, the IFNULL
function is not available, but it has an equivalent function called COALESCE
. It works in a similar way as IFNULL
in other databases.