m CREATE TRIGGER check_room_availability_trigger BEFORE INSERT ON has_BookingRel FOR EACH ROW BEGIN DECLARE available_rooms INT; SELECT room_num - IFNULL(SUM(booked_quantity), 0) INTO available_rooms FROM room_types LEFT JOIN has_BookingRel ON room_types.id = has_BookingRel.room_type_id WHERE room_types.id = NEW.room_type_id; GROUP BY room_types.id, room_types.name, room_types.price; IF available_rooms < NEW.booked_quantity THEN SIGNAL SQLSTATE “45000” SET MESSAGE_TEXT = ‘房间数量不足’; END IF; END;1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY room_types.id, room_types.name, room_types.price; IF available_rooms ’ at line 12
时间: 2023-07-15 08:14:04 浏览: 127
availability.rar_Free space_communication_free space optical_opt
在该触发器中,语法有误。出现错误的位置是在查询语句后的 GROUP BY 子句,因为在条件判断语句之前不能有 GROUP BY 子句。
以下是修改后的代码:
```
CREATE TRIGGER check_room_availability_trigger
BEFORE INSERT ON has_BookingRel
FOR EACH ROW
BEGIN
DECLARE available_rooms INT;
SELECT room_num - IFNULL(SUM(booked_quantity), 0) INTO available_rooms
FROM room_types
LEFT JOIN has_BookingRel ON room_types.id = has_BookingRel.room_type_id
WHERE room_types.id = NEW.room_type_id;
IF available_rooms < NEW.booked_quantity THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '房间数量不足';
END IF;
END;
```
修改后的代码已经删除了 GROUP BY 子句,应该可以正常工作了。
阅读全文