gpt4 book ai didi

mysql - 我需要构建一个触发器,当已经为包机分配飞行员时,将飞行员分配为副飞行员

转载 作者:行者123 更新时间:2023-11-29 18:20:14 25 4
gpt4 key购买 nike

DROP TRIGGER IF EXISTS COPILOT_ASSIGNED;

DELIMITER //
CREATE TRIGGER COPILOT_ASSIGNED
BEFORE INSERT ON crew
FOR EACH ROW
BEGIN
IF CHAR_TRIP EXISTS AND CREW_JOB = 'Pilot'
THEN SET NEW.CREW_JOB = 'Copilot';
END IF;
END //
DELIMITER ;

INSERT INTO crew VALUES (10019, 109, 'Pilot');
INSERT INTO crew VALUES (10019, 106, 'Pilot');

SELECT * FROM crew WHERE CHAR_TRIP = 10019;

我无法确定如何编写此触发器。我需要检查是否已为包机(CREW_TRIP)分配了飞行员(CREW_JOB)。如果是,则需要将飞行员更改为副驾驶(CREW_JOB)。

最佳答案

DROP TRIGGER IF EXISTS COPILOT_ASSIGNED;

DELIMITER //
CREATE TRIGGER COPILOT_ASSIGNED
BEFORE INSERT ON crew
FOR EACH ROW
BEGIN
IF EXISTS (SELECT * FROM crew WHERE CHAR_TRIP = NEW.CHAR_TRIP AND
CREW_JOB='Pilot')
THEN SET NEW.CREW_JOB = 'Copilot';
END IF;
END //
DELIMITER ;

INSERT INTO crew VALUES (10019, 109, 'Pilot');
INSERT INTO crew VALUES (10019, 106, 'Pilot');

关于mysql - 我需要构建一个触发器,当已经为包机分配飞行员时,将飞行员分配为副飞行员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46652316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com