[英]update sql table based on value in another table in PHP
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. 两个表中的order_id值相同。 I would like to update Stat from 'waiting' to 'done' in Orders table if product_id is '54' in Product table.
如果Product表中的product_id为'54',我想将Orders表中的Stat从'waiting'更新为'done'。
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 :) 将感谢您的帮助:)
You need to use JOIN something as 您需要使用JOIN作为
update orders
join product on product.order_id = orders.order_id
set
orders.Stat='done'
where product.product_id = '54'
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'");
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.