我有一个表(700 多行),格式如下:
id | session | win
------------------
1 1122 1
2 1122 1
3 1122 1
4 4559 1
5 4559 1
6 4559 1
7 4559 1
8 4559 1
其中 win
为 1
或 0
。
并且 session
并不是唯一的。
我想要更新具有相同 session
的行组,并将其所有 win
字段设置为 0
(1 除外) .
目标:
id | session | win
------------------
1 1122 1
2 1122 0
3 1122 0
4 4559 1
5 4559 0
6 4559 0
7 4559 0
8 4559 0
最佳答案
有几种方法将它们全部设置为 0,然后将每个 session 的最小值更新为 1。
Update table set win = 0
update table set win = 1 where ID in (Select min(ID) from table group by session)
或者 全部合一
Update table set win=0 where ID not in (Select MIN(ID) from table group by session)
关于MySql:如何从包含重复字段的行的表中更新单行出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10164514/