插入触发器后的mysql更新另一个表的列

2022-01-17 00:00:00 sql-update mysql triggers

我正在尝试编写触发器,我有以下表格:预订请求:

i'm trying to write a trigger, I have following tables: BookingRequest:

  +-----------+---------+------+-----+---------+----------------+
    | Field     | Type    | Null | Key | Default | Extra          |
    +-----------+---------+------+-----+---------+----------------+
    | idRequest | int(11) | NO   | PRI | NULL    | auto_increment |
    | roomClass | int(11) | NO   |     | NULL    |                |
    | inDate    | date    | NO   |     | NULL    |                |
    | outDate   | date    | NO   |     | NULL    |                |
    | numOfBeds | int(11) | NO   |     | NULL    |                |
    | status    | int(11) | NO   | MUL | NULL    |                |
    | idUser    | int(11) | NO   | MUL | NULL    |                |
    +-----------+---------+------+-----+---------+----------------+

状态表:

+------------+--------------------------------------------------+------+-----+---------+-------+
| Field      | Type                                             | Null | Key | Default | Extra |
+------------+--------------------------------------------------+------+-----+---------+-------+
| idStatus   | int(11)                                          | NO   | PRI | NULL    |       |
| nameStatus | enum('underConsideration','approved','rejected') | YES  |     | NULL    |       |
+------------+--------------------------------------------------+------+-----+---------+-------+

占用房间:

+--------------+---------+------+-----+---------+----------------+
| Field        | Type    | Null | Key | Default | Extra          |
+--------------+---------+------+-----+---------+----------------+
| idOccupation | int(11) | NO   | PRI | NULL    | auto_increment |
| idRoom       | int(11) | NO   |     | NULL    |                |
| idRequest    | int(11) | NO   |     | NULL    |                |
+--------------+---------+------+-----+---------+----------------+

如果将具有相同 id 的请求插入到 OccupiedRoom 表中,我需要一个触发器,它将 BookingReques 中的状态更改为 1,所以我尝试了类似的操作

i need a trigger which will change status in BookingReques to 1 if request with the same id is inserted into OccupiedRoom table, so i tried something like this

create trigger occupy_trig after insert on OccupiedRoom 
for each row
begin
  if BookingRequest.idRequest= NEW.idRequest
   then
       update BookingRequest
       set status = '1';
       where idRequest = NEW.idRequest;

    end if;
END;

它不起作用,所以任何建议都会非常受欢迎

and it doesn't work, so any suggestions would be very appriciated

推荐答案

试试这个:

DELIMITER $$
CREATE TRIGGER occupy_trig
AFTER INSERT ON `OccupiedRoom` FOR EACH ROW
begin
       DECLARE id_exists Boolean;
       -- Check BookingRequest table
       SELECT 1
       INTO @id_exists
       FROM BookingRequest
       WHERE BookingRequest.idRequest= NEW.idRequest;

       IF @id_exists = 1
       THEN
           UPDATE BookingRequest
           SET status = '1'
           WHERE idRequest = NEW.idRequest;
        END IF;
END;
$$
DELIMITER ;

相关文章