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;所以这个触发器该怎样改:1140 - In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'h.room_types.room_num'; this is incompatible with sql_mode=only_full_group_by
时间: 2023-07-15 15:13:55 浏览: 240
br-ga-pr.zip_BR_pr_ga_zip
这个错误是由于 MySQL 5.7.5 版本之后默认启用了 only_full_group_by 模式,而在该模式下,如果 SELECT 语句中的列没有在 GROUP BY 子句中出现,那么 MySQL 将会报错。在你的触发器代码中,SELECT 语句中的 room_num 列没有在 GROUP BY 子句中出现,因此会报这个错误。你需要将这个 SELECT 语句中的列改为聚合函数,比如使用 SUM()。
改为以下代码:
```
CREATE TRIGGER check_room_availability_trigger BEFORE INSERT ON has_BookingRel FOR EACH ROW
BEGIN
DECLARE available_rooms INT;
SELECT room_types.room_num - IFNULL(SUM(has_BookingRel.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, room_types.room_num;
IF available_rooms < NEW.booked_quantity THEN
SIGNAL SQLSTATE “45000” SET MESSAGE_TEXT = ‘房间数量不足’;
END IF;
END;
```
这样改动后,应该就可以避免这个错误了。
阅读全文