2

我有这张桌子叫shop

+---------+---------+------------+----------+
| shop_id | item_id | item_price | item_qty |
+---------+---------+------------+----------+
|       1 |       1 |          0 |       99 |
|       2 |       2 |          0 |       99 |
|       3 |       3 |          0 |       99 |
|       4 |       4 |          0 |       99 |
|       5 |       5 |          0 |       99 |
|       6 |       6 |          0 |       99 |
|       7 |       7 |          0 |       99 |
|       8 |       8 |          0 |       99 |
+---------+---------+------------+----------+

我也有这张名为item的表:

+---------+-----------------+-----------+----------+----------+----------+---------------------------------+-----------+------------+
| item_id | item_name       | item_type | item_atk | item_def | item_atr | item_img                        | item_desc | item_price |
+---------+-----------------+-----------+----------+----------+----------+---------------------------------+-----------+------------+
|       0 | Halberd         |         1 |      220 |       20 |        0 | pics/weapons/halberd.png        |           |        400 |
|       1 | Axe             |         1 |      220 |       10 |        0 | pics/weapons/axe.png            |           |        200 |
|       2 | Wooden Sword    |         1 |       70 |        0 |        0 | pics/weapons/wooden-sword.png   |           |        225 |
|       3 | Dagger          |         1 |       60 |        5 |        0 | pics/weapons/dagger.png         |           |         55 |
|       4 | Bow             |         1 |      120 |        1 |        0 | pics/weapons/bow.png            |           |        120 |
|       5 | Helmet          |         4 |        0 |       50 |        0 | pics/headgears/helmet.png       |           |        155 |
|       6 | Tunic           |         2 |       10 |       10 |        0 | pics/armors/tunic.png           |           |         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 |
+---------+-----------------+-----------+----------+----------+----------+---------------------------------+-----------+------------+

我主要想这样做:根据item表的 *item_price* 更新shop表的 *item_price* 。

免责声明

询问此查询的目的是从 item_price 表中填写我的商店 item_price。但在未来商店 item_price 将具有与 item_price 不同的值。

例如:

从商店购买的物品将使用商店 item_price。
出售给商店的商品将基于 item_price。

4

1 回答 1

4

您可以通过加入表格来简单地做到这一点。

UPDATE  shop a
        INNER JOIN item b
            ON b.item_ID = a.item_ID
SET     a.item_price = b.item_price 

UPDATE语句执行后的输出

╔═════════╦═════════╦════════════╦══════════╗
║ SHOP_ID ║ ITEM_ID ║ ITEM_PRICE ║ ITEM_QTY ║
╠═════════╬═════════╬════════════╬══════════╣
║       1 ║       1 ║        200 ║       99 ║
║       2 ║       2 ║        225 ║       99 ║
║       3 ║       3 ║         55 ║       99 ║
║       4 ║       4 ║        120 ║       99 ║
║       5 ║       5 ║        155 ║       99 ║
║       6 ║       6 ║         50 ║       99 ║
║       7 ║       7 ║        150 ║       99 ║
║       8 ║       8 ║        199 ║       99 ║
╚═════════╩═════════╩════════════╩══════════╝
于 2013-05-02T08:40:59.793 回答