下面是代码 接下来的想法:在表订单中插入记录后,我需要获取last_orderid,对于该orderid,我需要来自同一表的餐厅名称,使用功能和餐厅名称,我将获得驾驶员姓名作为函数输出,对于该驾驶员姓名,我将找到车号在表驱动程序中,并且如果is_delivery = Yes,则该值希望以表顺序插入car_number列中。
我尝试最初更改表,但是主要问题是我需要功能的精确餐厅名称。
CREATE DEFINER=root@localhost TRIGGER select_driver AFTER INSERT ON orders
FOR EACH ROW
BEGIN
set @orderid = (select max(order_id) from orders);
SET @restauranatname=(select Restaurant_Name from orders WHERE order_id=@orderid);
set @drivername = (SELECT find_lazy_driver('@restauranatname'));
SET @car_number = (SELECT car_number FROM driver WHERE driver_name=@drivername);
update orders
SET car_number= @car_number
WHERE Order_ID = @orderid;
END
下面的代码在SQL中效果很好,但是在任何触发器中都会产生问题-我在插入以及更新之前/之后都尝试过
set @orderid = (select max(order_id) from orders);
SET @restauranatname=(select Restaurant_Name from orders WHERE
order_id=@orderid);
set @drivername = (SELECT `find_lazy_driver`(@restauranatname));
SET @car_number = (SELECT car_number FROM driver WHERE
driver_name=@drivername);
UPDATE orders
SET car_number= @car_number
WHERE order_id=@orderid ;