php - Update a column a if null else update column b else if both column's are not null do nothing -
this table: called room
+---------+---------+-----------+-------------+-------------+-------------+---------+ | 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 id of player wants join
#room table:
- if player_a_id !null , player_b_id !null update nothing; if
- player_a_id = null , player_b_id !null update room set
- player_a_id = $player_id; if player_b_id = null , player_a_id !null update room set player_b_id = $player_id;
my current query(thanks jw)(i tried edit no avail failed).
update room set player_a_id = if(player_a_id null or player_a_id = 0 , player_b_id != :chara_id, :chara_id, player_a_id), player_b_id = if(player_a_id != :chara_id , player_b_id not null, :chara_id, player_b_id) room_id = :room_id
this updates 2 columns if both empty or = 0; want update 1.
edit:
here sample result: after player_id 1 joins 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 | +---------+---------+-----------+-------------+-------------+-------------+---------+
since both columns null updates both of column want update 1 column.
reviewing logic, want update player_a_id $player_id if player_a_id = 0 , player_b_id not = 0.
and want update player_b_id $player_id if player_b_id = 0 , player_a_id not = 0.
edit -- want update player_a_id if both equal 0.
update room set player_a_id = if(player_a_id=0, :player_id, player_a_id), player_b_id = if(player_a_id!=0 , player_b_id=0, :player_id, player_b_id) room_id = :room_id
Comments
Post a Comment