【发布时间】:2021-11-25 02:27:25
【问题描述】:
我有这个问题:
update client
set start_date = current_date,
email = '123@fakeemail.com'
where client_id = 1
returning client_id, username, 1 as isSuccess
更新成功后返回如下:
| client_id | username | isSuccess |
|---|---|---|
| 1 | test_name | 1 |
更新不执行时,返回client_id、username和isSuccess,但它们的值为空。
我遇到的问题是自定义不执行更新时返回的内容。如果没有执行更新,我需要返回以下内容:
| client_id | username | isSuccess |
|---|---|---|
| NULL | NULL | 0 |
在不执行更新时,编写带有 ELSE 子句的 RETURNING 子句以获取上述结果集有什么技巧吗?或者还有其他方法可以获得我需要的结果集吗?以下代码不起作用 -
update client
set start_date = current_date,
email = '123@fakeemail.com'
where client_id = 1
returning client_id, username, 1 as isSuccess
else client_id is null, username is null, 0 as isSuccess
【问题讨论】:
-
根据 PostgreSQL 文档“可选的 RETURNING 子句导致 UPDATE 根据实际更新的每一行计算和返回值。”如果没有行,那么它将仅返回 null。
标签: sql postgresql sql-update