Mysql how to update multiple records without specifying foreignkey value

S Singh :

I have a tables that looks like the following:

Table - order
id purchase_order_item_id amount
1  1324                   0.0
2  2435                   50.2
3  5643                   87.2
4  6475                   0.0

Table - purchase_order_item
id                     item_id
1324                   82
2435                   83
5643                   84
6475                   85

Table - item
id                     amount
82                   76.1
83                   50.2
84                   87.2
85                   65.9

Now I want to check if Order table is having value 0.0 for amount. In this case I have to get amount data from table item (Order->purchase_order_item->item) and update in order table.

I have written below update query which is not running.Trying to correct it.

UPDATE order
INNER JOIN purchase_order_item ON purchase_order_item.id =     order.purchase_order_item_id
SET 
order.amount= 
(
SELECT item.amount from item
INNER JOIN purchase_order_item ON purchase_order_item.item_id=item.id
)
where order.amount=0.0;

I am new in mysql query and trying to write correct update query.

GMB :

I think that you want the update ... join syntax:

update orders o
inner join purchase_order_items poi on poi.id = p.purchas_order_item_id
inner join items i on i.id = poi.item_id
set o.amount = i.amount
where o.amount = 0

Side note: order is a reserved word in MySQL (as for other databases), so I renamed the table to orders (I pluralized other table names too to make it consistent).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360347&siteId=1