gpt4 book ai didi

mysql - 如何在一个 sql 中更新唯一的默认选项

转载 作者:行者123 更新时间:2023-11-29 01:34:41 24 4
gpt4 key购买 nike

我有这样的 table

-----------------------
id | name | is_default|
------------------------
1 | a | 1 |
2 | a | 0 |
3 | a | 0 |
4 | a | 0 |
-----------------------

现在我想同时将 line 2(id =2) is_default 更改为 1,并将 origin line(id =1) id_default 更改为 0,就像在 UI 列表中选择默认选项一样。1.我可以在一个sql语句中做到这一点吗?2.如果可以,sql语句怎么写或者mybatis mapper.xml里怎么写?

Springboot与mybatis,sql语句写入mapper.xml

@Data
pulbic class Option{
private Integer id;
private String name;
private Boolean isDefault;
}

mybatis或者mysql的语句怎么写?

最佳答案

您可以使用 CASE 表达式:

UPDATE yourTable
SET is_default = CASE WHEN id = 1 THEN 0 ELSE 1 END
WHERE id IN (1, 2);

或者,如果您只想切换 id 1 和 2 的默认值,请尝试:

UPDATE yourTable
SET is_default = CASE WHEN is_default = 1 THEN 0 ELSE 1 END
WHERE id IN (1, 2);

关于mysql - 如何在一个 sql 中更新唯一的默认选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57268639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com