如果数据存在则交换不同表中的值然后如果data = 0则值+ 1然后删除

时间:2013-05-06 01:54:09

标签: php mysql sql pdo swap

我的这张桌子名为 bag

+--------+----------+---------+----------+
| bag_id | chara_id | item_id | item_qty |
+--------+----------+---------+----------+
|      1 |        1 |       2 |       26 |
|      2 |        1 |       1 |       56 |
|      3 |        3 |       1 |        4 |
|      6 |        3 |       4 |        3 |
|      7 |        4 |       4 |        3 |
|      8 |        5 |       4 |        3 |
|      9 |        6 |       4 |        2 |
|     10 |        1 |       5 |        1 |
|     14 |        1 |       8 |        1 |
|     15 |        1 |       6 |        2 |
|     18 |        1 |       4 |        4 |
|     19 |        1 |       3 |        2 |
|     29 |        8 |       1 |        1 |
|     30 |        8 |       7 |        2 |
|     33 |        6 |       2 |        1 |
|     34 |        3 |       5 |        1 |
|     35 |        3 |       8 |        3 |
|     37 |        4 |       3 |        1 |
|     45 |        3 |       3 |       14 |
|     46 |        8 |       2 |        2 |
|     60 |        8 |       5 |        2 |
|     61 |        3 |       2 |       10 |
|     74 |       12 |       2 |        1 |
|     97 |       12 |       5 |        1 |
|    103 |        3 |       6 |        1 |
+--------+----------+---------+----------+

并将此表称为设备

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

情形: 玩家装备从包到设备的物品:

#unequipping:
1. if equipment id to be unequipped exist in the bag then that bag item = item + 1
   else
   insert that item to the bag

#equipping:   
1.  bag item = bag item - 1 where item_id = equipped item_id
    else if bag item = 0 then delete it.

基本上我想将物品从设备换到袋子表, 虽然扭曲是数量,其中如果要交换的项目已经存在于包表中,那么+ 1到数量,如果包表中的项目是= 1,则从表格中删除该项目并装备该项目设备表。

如果你无法理解我的情况,请告诉我,它让我疯狂!

this is my current code(if it helps a little):

$fieldnames   = array(1=>'weapon', 'armor', 'ring', 'headgear');

        if (isset($fieldnames[$equip->item_type])) {
            $field = $fieldnames[$equip->item_type].'_id';

            $sql_unequip  = "INSERT INTO bag(item_id, chara_id, item_qty)VALUES(:item_id, :chara_id, 1) ON DUPLICATE KEY UPDATE item_qty = item_qty + 1";
            $sql_equip    = "UPDATE equipment SET $field   = :item_id WHERE chara_id = :chara_id";
            $sql_unequip2 = "DELETE FROM bag WHERE item_id = :item_id AND chara_id   = :chara_id AND item_qty = 0";


        }

0 个答案:

没有答案