有两个表:
orders
____________________
order_id | Stat
--------------------
1 | waiting
2 | waiting
3 | waiting
第二张表:
product
____________________
order_id | product_id
---------------------
1 | 53
2 | 54
3 | 54
两个表中的order_id值相同。我想更新等待'完成'在订单表中,如果product_id是' 54'在产品表中。
我尝试了这段代码,但没有成功:
mysql_query("UPDATE orders SET stat='done' FROM product WHERE product_id='54'");
非常感谢您的帮助:)
答案 0 :(得分:5)
您需要使用JOIN作为
update orders
join product on product.order_id = orders.order_id
set
orders.Stat='done'
where product.product_id = '54'
答案 1 :(得分: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'");
答案 2 :(得分:0)
如果您希望将此逻辑嵌入到数据库中,您可以使用更新触发器(如下面的伪代码):
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;