我试图找到一个匹配数据库中的商品和请求的触发器,如果匹配则将数据添加到另一个表中。这对我来说变得非常复杂,也许有人可以指出我正确的方向。 这就是我到目前为止所拥有的:
CREATE TRIGGER rideMatch
AFTER INSERT ON rideRequest
FOR EACH ROW
BEGIN
IF EXISTS(
SELECT * from rideOffer r
where r.time = new.time AND
r.date = new.date AND
r.pickUpLocation = new.pickUpLocation AND
r.dropOffLocation = new.dropOffLocation
)
THEN
INSERT INTO Ride(rideDate, pickUpTime, pickUpLocation, dropOffLocation, driver, carName)
VALUES(new.date, new.time, new.pickUpLocation, new.dropOffLocation, r.driver, r.carName);
END IF;
答案 0 :(得分:0)
首先,我不认为这种逻辑应该在触发器中完成。它似乎是应用程序逻辑,而不是数据完整性。存储过程似乎更合适。
也就是说,您需要从商品表中获取数据。我想是这样的:
VisualStateManager
注意:
CREATE TRIGGER rideMatch
AFTER INSERT ON rideRequest
FOR EACH ROW
BEGIN
insert into Ride(rideDate, pickUpTime, pickUpLocation, dropOffLocation, driver, carName)
select new.date, new.time, new.pickUpLocation, new.dropOffLocation,
r.driver, r.carName
from rideOffer r
where r.time = new.time AND
r.date = new.date AND
r.pickUpLocation = new.pickUpLocation AND
r.dropOffLocation = new.dropOffLocation
limit 1;
end;
。rideOffer
最终可能会用于多个请求。rideOffer
中的主键而不是输入字段值。换句话说,这应该回答你的问题。但是,我认为应用程序设计存在重大缺陷 - 缺陷超出了本问题的范围。