UPDATE all column values equivalent to another tables column value based on their id

前端 未结 1 1588
不知归路
不知归路 2021-01-15 01:31

I have this table called shop:

+---------+---------+------------+----------+
| shop_id | item_id | item_price | item_qty |
+---------+--         


        
相关标签:
1条回答
  • 2021-01-15 01:43

    You can simply do this by joining the tables.

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

    OUTPUT after the UPDATE statement has been executed

    ╔═════════╦═════════╦════════════╦══════════╗
    ║ 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 ║
    ╚═════════╩═════════╩════════════╩══════════╝
    
    0 讨论(0)
提交回复
热议问题