【问题标题】:Update a column a if null else update column b else if both column's are not null do nothing如果两列都不为空,则更新 a 列,否则更新列 b,否则不执行任何操作
【发布时间】:2013-05-10 01:15:08
【问题描述】:

这是我的桌子: 叫房间

+---------+---------+-----------+-------------+-------------+-------------+---------+
| 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 table:

  1. 如果 player_a_id !null 和 player_b_id !null 则不更新;如果
  2. player_a_id = null and player_b_id !null 然后更新房间设置
  3. 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 |
+---------+---------+-----------+-------------+-------------+-------------+---------+

因为两列都为空,所以它更新两列我只想更新一列。

【问题讨论】:

  • 你能提供一些样本数据和结果吗?这将有助于澄清您的规则。
  • 鉴于您的编辑,如果两个播放器均为 0(均为空),您会更新哪个播放器?

标签: php mysql sql pdo


【解决方案1】:

查看您的逻辑,您希望将 player_a_id 更新为 $player_id IF player_a_id = 0 而 player_b_id 不 = 0。

并且您想将 player_b_id 更新为 $player_id IF player_b_id = 0 而 player_a_id 不 = 0。

EDIT -- 如果它们都等于 0,您还想更新 player_a_id。

UPDATE  room
SET     player_a_id = IF(player_a_id=0, :player_id, player_a_id),
    player_b_id = IF(player_a_id!=0 AND player_b_id=0, :player_id, player_b_id)
WHERE   room_id  = :room_id

【讨论】:

  • @sgeddess 是的,但是再次扭曲的是,当 player_a_id 和 player_b_id 都为 null 时,我想在 player_a_id 上插入 player_id,您的查询将起作用,但只有在 1 个玩家是已经在一个房间里。如果你能帮我解决这个问题,我会很感激的。
  • @Viscocent -- 为什么不将第一个 IF 语句更改为 player_a_id=0 -- 这应该涵盖两种情况?
  • 我已经知道了。刚刚在 if#1 player_b_id != :chara_id 和 if#2 player_a_id != :chara_id 上添加了这个条件
  • 谢谢顺便说一句,当你用 0 替换那些 IF NULL 语句时,我得到了逻辑工作。
  • @Viscocent -- np,很高兴我们能一起解决这个问题!最好的问候。
猜你喜欢
  • 2021-02-26
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多