簡體   English   中英

從2個表中選擇項的總值,並更新另一個表的值(COMPLICATED QUERY)

[英]Selecting Total value of items from 2 tables and update value of another TABLE (COMPLICATED QUERY)

我的情況是我想計算要在我制作的戰斗模塊上使用的玩家攻擊力,但我只是想知道我實際上有2個選擇:

計算服務器造成的損害。(我當前的選擇)

  1. 使用PHP計算DAMAGE DEALT和UPDATE服務器數據庫值。
  2. 傳遞2個chara id,然后在QUERY和UPDATE all中計算所有值(這是可能的)。

問題:我可以在查詢中這樣做嗎(選項B)

我當前的設置:
1個角色有4個物品,我通過在客戶端將所有4個物品atk和chara基本at​​k相加來計算角色atk。 (我認為這很容易出現安全漏洞),然后在服務器端更新值。

這是我的桌子:

角色:

+----------+------------+----------------+-------------+------------+----------+----------+----------+-----------+-----------+
| chara_id | chara_name | chara_class_id | chara_level | chara_gold | chara_hp | chara_mp | chara_xp | chara_atk | chara_def |
+----------+------------+----------------+-------------+------------+----------+----------+----------+-----------+-----------+
|        1 | LawrenceX  |              1 |           5 |        230 |     -175 |     1000 |        0 |         7 |         3 |
|        3 | Viscocent  |              2 |           2 |         96 |     -206 |     1100 |     1700 |         5 |         5 |
|        4 | Piatos     |              1 |           1 |        120 |      -60 |     1000 |        0 |         7 |         3 |
|        5 | Hello      |              1 |           1 |        300 |      -50 |     1000 |      200 |         2 |         8 |
|        6 | Sample     |              3 |           2 |        251 |      -85 |      900 |        0 |         9 |         1 |
|        8 | Sampuro    |              2 |           1 |        170 |      895 |     1100 |      700 |         5 |         5 |
|       12 | fail       |              2 |           3 |        481 |     1100 |     1300 |        0 |        21 |         9 |
|       13 | new        |              1 |           1 |       1000 |      -80 |     1000 |        0 |         5 |         5 |
+----------+------------+----------------+-------------+------------+----------+----------+----------+-----------+-----------+

項目:

+---------+-----------------+-----------+----------+----------+----------+---------------------------------+-------------------------------------------------+------------+
|       0 | None            |         0 |        0 |        0 |        0 | pics/none.png                   |                                                 |        400 |
|       1 | Axe             |         1 |      220 |       10 |        0 | pics/weapons/axe.png            | Another lumberjack axe is another man's weapon. |        200 |
|       2 | Wooden Sword    |         1 |       70 |        0 |        0 | pics/weapons/wooden-sword.png   | A wooden sword, 99% made from wood              |        225 |
|       3 | Dagger          |         1 |       60 |        5 |        0 | pics/weapons/dagger.png         | A Dagger, Cheap and Sharp                       |         55 |
|       4 | Bow             |         1 |      120 |        1 |        0 | pics/weapons/bow.png            | The basics and simplest of all bows.            |        120 |
|       5 | Helmet          |         4 |        0 |       50 |        0 | pics/headgears/helmet.png       | iron helmet - made from an iron pot scraps.     |        155 |
|       6 | Tunic           |         2 |       10 |       10 |        0 | pics/armors/tunic.png           | A peasants tunic.                               |         50 |
|       7 | Armour          |         2 |        0 |       75 |        0 | pics/armors/armour.png          |                                                 |        150 |
|       8 | Necklace        |         3 |       25 |       15 |        0 | pics/accessories/necklace.png   |                                                 |        199 |
|       9 | Studded Leather |         2 |       25 |       60 |        0 | pics/armors/studded-leather.png |                                                 |        240 |
+---------+-----------------+-----------+----------+----------+----------+---------------------------------+-------------------------------------------------+------------+

設備:

+----------+----------+-----------+-------------+----------+---------+
| equip_id | chara_id | weapon_id | headgear_id | armor_id | ring_id |
+----------+----------+-----------+-------------+----------+---------+
|        3 |        1 |        14 |           5 |        6 |       8 |
|        5 |        3 |         4 |           5 |        6 |       8 |
|        6 |        4 |        11 |           5 |        7 |       8 |
|        7 |        5 |        12 |           5 |        6 |       8 |
|        8 |        6 |         3 |          16 |        7 |       8 |
|       10 |        8 |        15 |           5 |        7 |       8 |
|       13 |       12 |        14 |           5 |        6 |      17 |
|       40 |       13 |         3 |           5 |        7 |       8 |
+----------+----------+-----------+-------------+----------+---------+

表關系:

1 chara = 1 equipment  
1 weapon_id, armor_id, ring_id, headgear_id = 1 item (total of 4 items, headgear_id = 1 item).

我可以使用此查詢(KUDOS @JC)來獲得角色的設備:

SELECT i1.item_atk weapon_atk,i1.item_def weapon_def,
       i2.item_atk headgear_atk,
       i2.item_def headgear_def,
       i3.item_atk armor_atk,
       i3.item_def armor_def,
       i4.item_atk ring_atk,
       i4.item_def ring_def
       FROM equipment e LEFT JOIN
       item i1 ON e.weapon_id = i1.item_id LEFT JOIN
       item i2 ON e.headgear_id = i2.item_id LEFT JOIN
       item i3 ON e.armor_id = i3.item_id LEFT JOIN
       item i4 ON e.ring_id = i4.item_id 
       WHERE e.chara_id = 1

結果:

+------------+------------+--------------+--------------+-----------+-----------+----------+----------+
| weapon_atk | weapon_def | headgear_atk | headgear_def | armor_atk | armor_def | ring_atk | ring_def |
+------------+------------+--------------+--------------+-----------+-----------+----------+----------+
|        275 |         25 |            0 |           50 |        10 |        10 |       25 |       15 |
+------------+------------+--------------+--------------+-----------+-----------+----------+----------+

現在我要總計該字符設備的atk和def並在該查詢中將其返回

預期成績:

+------------+------------+
| total_atk  | total_def  |
+------------+------------+
|        310 |        100 |
+------------+------------+

這是我能想到的最簡單的方法。

SELECT IFNULL(W.item_atk, 0) + IFNULL(H.item_atk, 0) + IFNULL(A.item_atk, 0) + IFNULL(R.item_atk, 0) AS total_atk
    , IFNULL(W.item_def, 0) + IFNULL(H.item_def, 0) + IFNULL(A.item_def, 0) + IFNULL(R.item_def, 0) AS total_def
    FROM equipment E
    LEFT JOIN item W ON W.item_id = E.weapon_id
    LEFT JOIN item H ON H.item_id = E.headgear_id
    LEFT JOIN item A ON A.item_id = E.armor_id
    LEFT JOIN item R ON R.item_id = E.ring_id
    WHERE E.chara_id = 1

我已將表的別名重命名以輕松跟蹤它們。 如果字符沒有特定的設備,我將使用IFNULL

================================================== ================================

杜德(Dude),我剛剛提出了另一個查詢,我認為這比上面的查詢要快。 雖然,我還沒有測試它們。

SELECT SUM(IFNULL(I.item_atk, 0)) AS total_atk
    , SUM(IFNULL(I.item_def, 0)) AS total_def
    FROM equipment E
    LEFT JOIN item I ON I.item_id = E.weapon_id
        OR I.item_id = E.headgear_id
        OR I.item_id = E.armor_id
        OR I.item_id = E.ring_id
    WHERE E.chara_id = 1
    GROUP BY E.chara_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM