1

我有这张叫做 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();

我的挣扎:

我如何将 +100 添加到 chara 表中 chara_hp 的当前值,其中 chara 表中的当前 chara_hp 不超过 chara_base_stat 表中的 chara_base_hp

基本上我想要的只是在当前 hp 小于 chara_base_stat 表中的当前 chara_base_hp 时才重新生成那些玩家的 hp

注意:当前的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 }
4

1 回答 1

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
于 2013-04-29T06:47:35.337 回答