Skip to content
Advertisement

update sql table from two tables Using a single query

i want to update a table column by taking i/p from two tables in a single query.

This is what i have tried.

   $query="UPDATE all_stores a
inner join i_v i on
    a.id = i.child_id
set a.qty = (a.qty-i.qty)";

But this is not updating the table.

Where Table-all_stores is:


   id-    name         qty -           dec_qty
    1      a1          68                0
    2      b2          32                0
    3      c3          12                0
    4      d4          43                0

And table-i_v is:

    id-    name        qty -          child_id
    1      a1          12               1
    2      a2          32               2
    3      a1          11               1
    4      a1          23               1

Advertisement

Answer

if I understand correctly , you want to do this :

UPDATE all_stores a
set a.qty = (a.qty-(select sum(qty) from i_v i where a.id = i.id))
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement