0

There are two tables:

       orders
____________________
order_id |   Stat
--------------------
   1     |  waiting
   2     |  waiting
   3     |  waiting

second table:

       product
____________________
order_id | product_id
---------------------
   1     |       53
   2     |       54
   3     |       54

order_id value is the same in both tables. I would like to update Stat from 'waiting' to 'done' in Orders table if product_id is '54' in Product table.

I tried this code but didn't work:

mysql_query("UPDATE orders SET stat='done' FROM product WHERE product_id='54'");

will appreciate your help :)

3
  • Please, please please stop using the mysql_* extension. It's deprecated, issues notices, is unsafe, not maintained and will be removed some time in the future. Use mysqli_* or PDO instead Commented Jun 18, 2014 at 12:41
  • Thanks for advising. Do you mean I should use it like this? mysqli_query Commented Jun 18, 2014 at 13:48
  • Yes, but you should learn the alternative extensions because, even though at first glance mysqli_* looks a lot like mysql_*, they don't translate 1 on 1: the new extensions are a bit more complicated, and a lot more powerful. Commented Jun 18, 2014 at 13:49

3 Answers 3

5

You need to use JOIN something as

update orders
join product on product.order_id = orders.order_id
set 
orders.Stat='done'
where product.product_id = '54'
Sign up to request clarification or add additional context in comments.

1 Comment

and also use order.Stat as it exists in the order table - so with a capital letter.
0
do this it work for u

mysql_query("UPDATE orders SET stat='done' where product_id in (select product_id from  product WHERE product_id='54'");

Comments

0

If you want this logic to be embedded in the DB you could use an update trigger like the one beneath (pseudo code):

CREATE TRIGGER upd_check BEFORE UPDATE ON product
FOR EACH ROW
BEGIN
    IF NEW.product_id = 54 THEN
        UPDATE orders SET Stat = 'done' WHERE order_id = NEW.order_id
    END IF;
END;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.