将postgresql更新语句减少到一行

时间:2017-08-31 15:07:23

标签: sql postgresql

如何将以下更新状态减少为一个?

e.g with IF-Clause:

  UPDATE users SET registered_at = now() WHERE id = new.id AND status = 'completed';
  UPDATE users SET registered_at = NULL WHERE id = new.id AND status != 'completed';

感谢

1 个答案:

答案 0 :(得分:1)

使用case表达式:

UPDATE users
SET registered_at = case when status = 'completed' then now() end
WHERE id = new.id

ELSE NULL是默认值,因此您无需指定case表达式的那一部分。