我有桌上游戏
id | visitor | score1 | home | score2 | winner
------+-----------+--------+----------+--------+----------
1 | abc | 20 | xyz | 15 |
2 | def | 31 | uvw | 29 |
3 | ghi | 30 | rst | 25 |
4 | jkl | 16 | lmn | 27 |
5 | lmn | 15 | opq | 21 |
6 | opq | 14 | jkl | 16 |
7 | rst | 9 | ghi | 41 |
8 | uvw | 10 | def | 21 |
9 | xyz | 23 | abc | 19 |
我如何迭代每一行并比较访问者和主队的分数,然后用获胜者的名字更新获胜者专栏?
答案 0 :(得分:2)
使用CASE
的定期更新做得很好;
UPDATE games
SET winner = CASE WHEN score1 > score2 THEN visitor
WHEN score2 > score1 THEN home
ELSE 'tie' END;