0

如果我的列 player_a_id 为空,我想更新它,否则更新 player_b_id。

这是我的桌子: 房间

+---------+---------+-------------+-------------+-------------+---------+
| room_id | room_no | room_name   | player_a_id | player_b_id | turn_of |
+---------+---------+-------------+-------------+-------------+---------+
|       1 |       1 | blah        |           1 |           3 |       0 |
|       2 |       5 | second room |           1 |           3 |       0 |
|       3 |       3 | 3rd room    |           4 |           5 |       0 |
|       4 |       4 | 4th room    |           6 |           7 |       0 |
+---------+---------+-------------+-------------+-------------+---------+

基本上我的目标很简单,当玩家加入房间时,它会更新 player_a_id,否则如果 player_a_id 已被占用,则更新 player_b_id。

4

1 回答 1

2

如果这个词的empty意思是NULL

UPDATE  room
SET     player_a_id = IF(player_a_id IS NULL OR player_a_id = 0, yourVal, player_a_id),
        player_b_id = IF(player_a_id IS NOT NULL OR player_a_id <> 0, yourVal, player_b_id)
WHERE   room_no  = '' // <<== (sample only) set your condition here....
于 2013-05-09T06:23:04.380 回答