当表格orders_foods = 1时,我希望表格订单的更新状态为1,其中order_id = order_id与更新相同。
CREATE TRIGGER upd_check AFTER UPDATE ON orders_foods
FOR EACH ROW
BEGIN
IF (new.status= '1' where old.order_id=new.order_id) THEN
update orders
set status= '1'
where old.order_id=old.order_id
END IF;
END;
订单表 orders_foods表
我不知道如何在更新过程中写入order_id值以触发并使其与订单表上的id相匹配
我的问题解决了
BEGIN
declare od_status_1 int default 0;
declare od_number int default 0;
select count(*) into od_number from orders_foods where orders_foods.order_id = new.order_id;
select count(*) into od_status_1 from orders_foods where orders_foods.order_id = new.order_id and orders_foods.status = 1;
if od_number = od_status_1 then
update orders
set status= '1'
where id=old.order_id;
end if;
END
鲑鱼到鲑鱼
答案 0 :(得分:0)
尝试一下
grunt version
答案 1 :(得分:0)
如果在所有order_detail状态都更改为1之前订单状态不应该更改,那么可能是这样的
drop trigger if exists t;
delimiter $$
CREATE TRIGGER t AFTER UPDATE ON od
FOR EACH ROW
BEGIN
declare od_number int default 0;
declare od_status_1 int default 0;
select count(*) into od_number from od where od.order_id = new.order_id;
select count(*) into od_status_1 from od where od.order_id = new.order_id and od.status = 1;
insert into debug_table(msg) values
(concat('new status:', new.status,' od found:',od_number,' od_status_1:',od_status_1)) ;
IF new.status= 1 THEN
insert into debug_table (msg) values
(concat('in status check new status:', new.status,' od found:',od_number,' od_status_1:',od_status_1) );
if od_number = od_status_1 then
update o
set status= '1'
where id=old.order_id;
end if;
END IF;
END $$
delimiter ;
请注意使用调试表来进行错误调试。
ariaDB [sandbox]> /*
/*> drop table if exists o,od;
/*>
/*> create table o(id int,status int);
/*> create table od(id int,order_id int,status int);
/*> */
MariaDB [sandbox]> truncate table o;
Query OK, 0 rows affected (0.19 sec)
MariaDB [sandbox]> truncate table od;
Query OK, 0 rows affected (0.21 sec)
MariaDB [sandbox]> insert into o values
-> (1,0),(2,0);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [sandbox]> insert into od values
-> (1,1,0),(2,1,0),(3,1,0),(4,2,0);
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0
MariaDB [sandbox]>
MariaDB [sandbox]> truncate table debug_table;
Query OK, 0 rows affected (0.34 sec)
MariaDB [sandbox]> update od set od.status = 1 where od.id = 1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [sandbox]> select * from o join od on od.order_id = o.id;
+------+--------+------+----------+--------+
| id | status | id | order_id | status |
+------+--------+------+----------+--------+
| 1 | 0 | 1 | 1 | 1 |
| 1 | 0 | 2 | 1 | 0 |
| 1 | 0 | 3 | 1 | 0 |
| 2 | 0 | 4 | 2 | 0 |
+------+--------+------+----------+--------+
4 rows in set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> update od set od.status = 1 where od.id in (2,4);
Query OK, 2 rows affected (0.02 sec)
Rows matched: 2 Changed: 2 Warnings: 0
MariaDB [sandbox]> select * from o join od on od.order_id = o.id;
+------+--------+------+----------+--------+
| id | status | id | order_id | status |
+------+--------+------+----------+--------+
| 1 | 0 | 1 | 1 | 1 |
| 1 | 0 | 2 | 1 | 1 |
| 1 | 0 | 3 | 1 | 0 |
| 2 | 1 | 4 | 2 | 1 |
+------+--------+------+----------+--------+
4 rows in set (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> select * from debug_table;
+----+-------------------------------------------------------+------+
| id | msg | MSG2 |
+----+-------------------------------------------------------+------+
| 1 | new status:1 od found:3 od_status_1:1 | NULL |
| 2 | in status check new status:1 od found:3 od_status_1:1 | NULL |
| 3 | new status:1 od found:3 od_status_1:2 | NULL |
| 4 | in status check new status:1 od found:3 od_status_1:2 | NULL |
| 5 | new status:1 od found:1 od_status_1:1 | NULL |
| 6 | in status check new status:1 od found:1 od_status_1:1 | NULL |
+----+-------------------------------------------------------+------+
6 rows in set (0.00 sec)
MariaDB [沙盒]>