Viscocent
Viscocent

Reputation: 2064

SELECT UPDATE with IF Statements in mysql

i have this table called 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 |
    +----------+------------+----------------+-------------+------------+----------+----------+--------------+--------------+-----------+-----------+

I have this chara_base_stat table:

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

and i have this script that updates the values of hp for every minute(using cronjob here):

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

My Struggle:

how can i add +100 to the current value of chara_hp on chara table where the current chara_hp in the chara table does not exceed in the chara_base_hp from chara_base_stat table

basically all i want is to only regenerate those players hp if it there current hp is lesser than of there current chara_base_hp from chara_base_stat table

Note: the current chara_base_hp corresponds to the current level of the player and chara_class_id of the player.

so far i have this query here:

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)

that returns

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

i want to do this in sql:

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

Upvotes: 1

Views: 117

Answers (1)

fthiella
fthiella

Reputation: 49079

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

Upvotes: 3

Related Questions