根据其id更新所有列值等效于另一个表列值

时间:2013-05-02 08:39:10

标签: php mysql sql pdo

我有一张名为 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_price *更新商店表的* item_price *。

声明

询问此查询的目的是从item_price表项填充我的shop item_price。但是将来商品item_price将与商品item_price具有不同的价值。

e.g:

从商店购买的商品将使用商品item_price 卖给商店的商品将基于item_price。

1 个答案:

答案 0 :(得分: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 ║
╚═════════╩═════════╩════════════╩══════════╝