使用mysql中的IF语句选择UPDATE

时间:2013-04-29 06:32:54

标签: php mysql sql pdo

我有一张名为chara的表:

    +----------+------------+----------------+-------------+------------+----------+----------+--------------+--------------+-----------+-----------+
    | chara_id | chara_name | chara_class_id | chara_level | chara_gold | chara_hp | chara_mp | chara_max_hp | chara_max_mp | chara_atk | chara_def |
    +----------+------------+----------------+-------------+------------+----------+----------+--------------+--------------+-----------+-----------+
    |        1 | LawrenceX  |              1 |           1 |          0 |    11610 |     1000 |            0 |            0 |         7 |         3 |
    |        2 | Testo      |              3 |           1 |          0 |    11465 |      900 |            0 |            0 |         9 |         1 |
    |        3 | Viscocent  |              2 |           1 |          0 |    11570 |     1100 |            0 |            0 |         5 |         5 |
    |        4 | Piatos     |              1 |           1 |          0 |    12470 |     1000 |            0 |            0 |         7 |         3 |
    |        5 | Hello      |              1 |           1 |          0 |    12600 |     1000 |            0 |            0 |         2 |         8 |
    |        6 | Sample     |              3 |           1 |          0 |    12700 |      900 |            0 |            0 |         9 |         1 |
    |        7 | tester     |              2 |           1 |          0 |    12500 |     1100 |            0 |            0 |         5 |         5 |
    |        8 | Sampuro    |              0 |           1 |          0 |    11700 |      100 |            0 |            0 |         5 |         5 |
    +----------+------------+----------------+-------------+------------+----------+----------+--------------+--------------+-----------+-----------+

我有这个chara_base_stat表:

    +--------------+----------------+------------------+---------------+---------------+----------------+----------------+
    | base_stat_id | chara_class_id | chara_base_level | chara_base_hp | chara_base_mp | chara_base_atk | chara_base_def |
    +--------------+----------------+------------------+---------------+---------------+----------------+----------------+
    |            1 |              1 |                1 |          1000 |          1000 |              5 |              5 |
    |            2 |              1 |                2 |          1100 |          1100 |             10 |             10 |
    |            3 |              1 |                3 |          1200 |          1200 |             15 |             15 |
    |            4 |              1 |                4 |          1300 |          1300 |             20 |             20 |
    |            5 |              1 |                5 |          1400 |          1400 |             25 |             25 |
    |            6 |              2 |                1 |           900 |          1100 |              7 |              3 |
    |            7 |              2 |                2 |          1000 |          1200 |             14 |              6 |
    |            8 |              2 |                3 |          1100 |          1300 |             21 |              9 |
    |            9 |              2 |                4 |          1200 |          1400 |             28 |             12 |
    |           10 |              2 |                5 |          1300 |          1500 |             35 |             19 |
    |           11 |              3 |                1 |          1100 |           900 |              2 |              8 |
    |           12 |              3 |                2 |          1200 |          1000 |              4 |             16 |
    |           13 |              3 |                3 |          1300 |          1100 |              6 |             24 |
    |           14 |              3 |                4 |          1400 |          1200 |              8 |             32 |
    |           15 |              3 |                5 |          1500 |          1300 |             16 |             40 |
    +--------------+----------------+------------------+---------------+---------------+----------------+----------------+

我有这个脚本每分钟更新一次hp的值(在这里使用cronjob):

$sql = "UPDATE chara SET chara_hp = chara_hp +100";
$stmt = $db->prepare($sql);
$stmt->execute();

我的奋斗:

如何在chara表中添加+100到chara_hp的当前值,其中chara表中的当前chara_hp在chara_base_hp表的chara_base_hp中没有超过

基本上我想要的只是重新生成那些玩家hp,如果它当前的hp小于当前chara_base_hp来自chara_base_stat表

注意:当前的chara_base_hp对应于玩家的当前等级和玩家的chara_class_id。

到目前为止,我在这里有这个问题:

SELECT ch.chara_hp, bs.chara_base_hp FROM chara ch inner join chara_base_stat bs on(ch.chara_class_id = bs.chara_class_id and ch.chara_level = bs.chara_base_level)

返回

+----------+---------------+
| chara_hp | chara_base_hp |
+----------+---------------+
|    13210 |          1000 |
|    14070 |          1000 |
|    14200 |          1000 |
|    13170 |           900 |
|    14100 |           900 |
|    13065 |          1100 |
|    14300 |          1100 |
+----------+---------------+

我想在sql中执行此操作:

if(chara_hp < chara_base_hp){ update chara set chara_hp = chara_hp + 100 }

1 个答案:

答案 0 :(得分:3)

UPDATE
  chara ch INNER JOIN chara_base_stat bs
  ON ch.chara_class_id = bs.chara_class_id
     AND ch.chara_level = bs.chara_base_level
SET
  ch.chara_hp = LEAST(bs.chara_base_hp, ch.chara_hp + 100)
WHERE
  ch.chara_hp < bs.chara_base_hp