這是我的表: 稱爲房間更新一列,如果空別的更新列b,否則如果兩個列的非空奈何
+---------+---------+-----------+-------------+-------------+-------------+---------+
| room_id | room_no | room_stat | room_name | player_a_id | player_b_id | turn_of |
+---------+---------+-----------+-------------+-------------+-------------+---------+
| 1 | 1 | 0 | blah | 0 | 0 | 0 |
| 2 | 5 | 0 | second room | 0 | 0 | 0 |
| 3 | 3 | 0 | 3rd room | 0 | 0 | 0 |
| 4 | 4 | 0 | 4th room | 0 | 0 | 0 |
+---------+---------+-----------+-------------+-------------+-------------+---------+
$player_id //contains the id of the player who wants to join
#room表:
- if player_a_id!null and player_b_id!null then update nothing; if
- player_a_id = null and player_b_id!null then update room set
- player_a_id = $ player_id;如果player_b_id = null並且player_a_id!null,則更新房間設置player_b_id = $ player_id;
我目前的查詢(謝謝JW)(我試圖編輯它,但沒有用我失敗)。
UPDATE room
SET player_a_id = IF(player_a_id IS NULL OR player_a_id = 0 AND player_b_id != :chara_id, :chara_id, player_a_id),
player_b_id = IF(player_a_id != :chara_id AND player_b_id IS NOT NULL, :chara_id, player_b_id)
WHERE room_id = :room_id
這是件好事,但如果兩者都爲空或= 0時更新兩列;我只想更新1
編輯:
這裏是樣品的結果: 後player_id 1聯接ROOM_ID 4:
+---------+---------+-----------+-------------+-------------+-------------+---------+
| room_id | room_no | room_stat | room_name | player_a_id | player_b_id | turn_of |
+---------+---------+-----------+-------------+-------------+-------------+---------+
| 1 | 1 | 0 | blah | 0 | 0 | 0 |
| 2 | 5 | 0 | second room | 0 | 0 | 0 |
| 3 | 3 | 0 | 3rd room | 0 | 0 | 0 |
| 4 | 4 | 0 | 4th room | 1 | 1 | 0 |
+---------+---------+-----------+-------------+-------------+-------------+---------+
因爲這兩個字段爲空,然後它會同時更新的列我只想更新1列。
你能提供一些樣本數據和結果嗎?這將有助於澄清你的規則。 – 2013-05-10 01:19:43
鑑於您的編輯,您更新的玩家是否都是0(均爲空)? – sgeddes 2013-05-10 01:35:43