postgresql Postgres:更新一个布尔列,将该列中的所有其他布尔值设置为 false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39910198/
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-10-21 02:24:39 来源:igfitidea点击:
Postgres: UPDATE a boolean column that sets all other booleans in that column to false
提问by Matt
I'm wondering if I can do this in one query
我想知道我是否可以在一个查询中做到这一点
Usecase: twitter pinned tweet. You can have at most one pinned tweet and setting a new pinned tweet, unset all the other previously pinned tweets.
用例:推特固定推文。您最多可以拥有一个固定推文并设置新的固定推文,取消设置所有其他先前固定的推文。
Any ideas?
有任何想法吗?
采纳答案by Jakub Kania
UPDATE tweets
SET pinned = NOT pinned
WHERE id = 1234 OR pinned = TRUE;
Or to be extra cautious
或者要格外小心
WHERE (id = 1234 AND pinned = FALSE) OR (pinned = TRUE AND id <> 1234)